I am writing a method to check if two int arrays are a rotation of each other. For example, [4,5,6,7,1,2] is a rotation of [1,2,4,5,6,7]
public static Boolean isRotation(int[] array1, int[] array2) {
boolean result = false;
if(array1.length != array2.length){
result = false;
}
ArrayList<Integer> alist = new ArrayList<Integer>();
LinkedList arr2 = new LinkedList(Arrays.asList(array2));
if(arr2.contains(array1[0])){
Iterator x = arr2.listIterator(arr2.indexOf(array1[0]));
while(x.hasNext()){
alist.add(x.next());
}
if(!(arr2.peek().equals(array1[0]))){
alist.add(arr2.peek());
arr2.pollFirst();
}
}//end of outer if loop.
//Compare the arraylist to array1;
for(int i = 0; i<array1.length; i++){
if(array1[i] != arr2.get(i)){
result = false;
}
}//end of for loop checking
return result;
}//end of method;
}//end of class;
However, I keep getting errors about object and integer.
In Line alist.add(x.next())
; and alist.add(arr2.peek());
I cannot use the add method because I get the (argument mismatch; Object cannot be converted to Integer) error;
and in this line if(array1[i] != arr2.get(i)){
I get the incomparable types: int and Object error.
I don't see why the object in arr2, the linkedlist isn't an integer. Can anyone please explain what is wrong?