Try this code.
char[] firstArray = {'a', 'b', 'c'};
char[] secondArray = {'a', 'b', 'c'};
if (firstArray.length != secondArray.length) {
System.out.println("False.they are not equal");
} else {
boolean isEqual = true;
for (int i = 0; i < firstArray.length; i++) {
if (firstArray[i] != secondArray[i]) {
System.out.println("False.they are not equal");
isEqual = false;
break;
}
}
if (isEqual)
System.out.println("true.they are equal");
}
Answer from Naman Gala on Stack OverflowTry this code.
char[] firstArray = {'a', 'b', 'c'};
char[] secondArray = {'a', 'b', 'c'};
if (firstArray.length != secondArray.length) {
System.out.println("False.they are not equal");
} else {
boolean isEqual = true;
for (int i = 0; i < firstArray.length; i++) {
if (firstArray[i] != secondArray[i]) {
System.out.println("False.they are not equal");
isEqual = false;
break;
}
}
if (isEqual)
System.out.println("true.they are equal");
}
boolean isEqual = true;
for(int i=0;i<arrayLen;i++){
if(firstArray[i] != secondArray[i]){
System.out.println("False.they are not equal");
isEqual = false;
break;
}
}
if(isEqual){
System.out.println("true.they are equal");
}
If that not work, try to change (firstArray[i] != secondArray[i]) to (!(firstArray[i].equals(secondArray[i]))) or st like that.
From what I see you just try to see if they are equal, if this is true, just go with something like this:
boolean areEqual = Arrays.equals(arr1, arr2);
This is the standard way of doing it.
Please note that the arrays must be also sorted to be considered equal, from the JavaDoc:
Two arrays are considered equal if both arrays contain the same number of elements, and all corresponding pairs of elements in the two arrays are equal. In other words, two arrays are equal if they contain the same elements in the same order.
Sorry for missing that.
use Arrays.equals(ary1,ary2); // returns boolean value
EDIT
you can use Arrays.deepEquals(ary1,ary2) to compare 2D arrays as well
also check this link for comparision comparision between Arrays.equls(ar1,ar2) and Arrays.deepEquals(ar1,ar2)
Java Arrays.equals() returns false for two dimensional arrays
EDIT 2
if you dont want to use these library methods then you can easily implement your method like this:
public static boolean ArrayCompare(int[] a, int[] a2) {
if (a==a2) // checks for same array reference
return true;
if (a==null || a2==null) // checks for null arrays
return false;
int length = a.length;
if (a2.length != length) // arrays should be of equal length
return false;
for (int i=0; i<length; i++) // compare array values
if (a[i] != a2[i])
return false;
return true;
}
Here is how you will compare your two double arrays for equality:
public static boolean compArray (double[] a, double[] b, int n) {
//if the arrays dont have the same length, then they are not equals
if (a.length != b.length) {
return false;
}
for (int i = 0; i < a.length; i++) {
//if one element at array a is different than the other at array b,
//at the same index, then the arrays are not equals
if (a[i] != b[i]) {
return false;
}
}
}
//otherwise, the two arrays are equals
return true;
}
Don't reinvent the wheel. There's a library function for that:
public static boolean compArray(double[] a, double[] b, int n) {
return Arrays.equals(a, 0, n, b, 0, n);
}
See Arrays.equalsโ(double[] a, int aFromIndex, int aToIndex, double[] b, int bFromIndex, int bToIndex)
I'm not 100% clear on how your algorithm is supposed to work, but it seems you may want to replace the line
pozita[i] = j;
with
pozita[i] = kodimi[j];
Right now, you are only writing the matching index to pozita, not a replacement character.
If my assumption is correct, you would also change
int[] pozita;
to
char[] pozita;
and initialize it to an array of length input.Length.
You never instantiated the array pozita. After you instantiate pozita, you can then start overriding the values in pozita. You are assigning j to posita[i] , posita is null.
Do something like:
int posita[] = new int[20]
and if you don't want to set size then just use an arraylist.