Reputation: 93
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?
Upvotes: 1
Views: 2127
Reputation: 59960
You are using generics in ArrayList :
ArrayList<Integer> alist = new ArrayList<Integer>();
Why not using it in :
LinkedList<Integer> arr2 = new LinkedList(Arrays.asList(array2));
and
Iterator<Integer> x = arr2.listIterator(arr2.indexOf(array1[0]));
Instead
LinkedList arr2 = new LinkedList(Arrays.asList(array2));
and
Iterator x = arr2.listIterator(arr2.indexOf(array1[0]));
Another solution(not useful in your case) without generics, you have to cast each Object :
alist.add((Integer) x.next());
^^^^^^^^^^^^^^^^^^
alist.add((Integer) arr2.peek());
^^^^^^^^^
if(array1[i] != (Integer) arr2.get(i)){
^^^^^^^^^
As @Aominè and the others mention you have several issues you have to fix them, I would just to solve your problem with another way :
public static Boolean isRotation(int[] array1, int[] array2) {
List<Integer> list1 = Arrays.stream(array1).boxed().collect(Collectors.toList());
List<Integer> list2 = Arrays.stream(array2).boxed().collect(Collectors.toList());
int size = list1.size();
for (int i = 0; i < size; i++) {
if (list1.equals(list2)) {
return true;
}
Collections.rotate(list2, -1);
}
return false;
}
Consider you have this two arrays (I consider that they are not empty and have the same length)
array1 = {4, 5, 6, 7, 1, 2}
array2 = {1, 2, 4, 5, 6, 7}
for each element in array1 move it to the end and check if it is equal with the second array of not :
First iteration :
array1 = {4, 5, 6, 7, 1, 2}
array2 = {1, 2, 4, 5, 6, 7}
not equal
Second iteration :
array1 = {4, 5, 6, 7, 1, 2}
array2 = {2, 4, 5, 6, 7, 1}//move 1 to the end
not equal
Second iteration :
array1 = {4, 5, 6, 7, 1, 2}
array2 = {4, 5, 6, 7, 1, 2}//move 2 to the end
Equal(and break)
If there are no match and the Iteration is end, then return false
mean not Equals
Upvotes: 2
Reputation: 9786
Because you didn't specify the Integer
type paramater:
LinkedList<Integer> arr2 = new LinkedList<>(Arrays.asList(array2));
Upvotes: 0
Reputation: 56423
There are several issues with your code:
result
variable is always false, which doesnt seem right. so double check your logic again.That said, let's solve the raw type issue as follows with the use of generics:
ArrayList<Integer> alist = new ArrayList<>();
LinkedList<Integer> arr2 = new LinkedList<>(Arrays.asList(array2));
if(arr2.contains(array1[0])){
Iterator<Integer> x = arr2.listIterator(arr2.indexOf(array1[0]));
...
...
At this point, there is still another issue,
the statement LinkedList<Integer> arr2 = new LinkedList<>(Arrays.asList(array2));
will not compile as array2
is of type int[]
so the receiver type should be LinkedList<int[]>
to make it compile but obviously that's not what you're after.
To solve the problem, you'll need to convert the int[]
. you can do this as follows:
LinkedList<Integer> arr2 = Arrays.stream(array2)
.boxed()
.collect(Collectors.toCollection(LinkedList::new));
Upvotes: 4