code to check two int arrays to see if mirrors of each other
Trying to make a method that takes in two int arrays then reports true if they are mirror images of each other, or false if they are not. Assuming both arrays are of the same length.
example:
data1:{1,2,3}
data2:{3,2,1}
==> true
data1:{1,2,3}
data2:{2,3,1}
==> false
everything I try is giving me an ArayIndexOutOfBoundsException with my "for loop".
This is the most recent code I tried:
Code :
public boolean mirrorImage(int[] data1, int[] data2){
boolean mirrors = true;
int x = 0;
int a = 0;
int b = data2.length;
while(x < data1.length){
if(data1[a] != data2[b]){
mirrors = false;
break;
}
else{
a = a + 1;
b = b - 1;
}
x = x + 1;
}
return mirrors;
}
Re: code to check two int arrays to see if mirrors of each other
Hello ksahakian21!
You should always print the variables which cause an exception to see what is going on. In your case it is quite obvious why you get an ArrayIndexOutOfBoundsException in your while loop. You assign data2.length to b and then you try to find data2[b]. Remember that arrays' indexes start at 0 and go to array.length-1.
For example a 10-length array goes from 0-9.
Hope I was clear.