繁体   English   中英

对象无法转换为整数错误

[英]Object cannot be converted to integer error

我正在编写一种方法来检查两个int数组是否彼此旋转。 例如,[4,5,6,7,1,2]是[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;

但是,我不断收到有关对象和整数的错误。 alist.add(x.next()) ; alist.add(arr2.peek()); 我无法使用add方法,因为出现了(参数不匹配;对象无法转换为Integer)错误; 在这一行中if(array1[i] != arr2.get(i)){我得到了无法比拟的类型:int和Object错误。

我不明白为什么arr2中的对象链表不是整数。 谁能解释什么是错的?

您的代码有几个问题:

  1. 使用原始类型会产生上述错误。
  2. result变量始终为false,似乎不正确。 因此,请再次仔细检查您的逻辑。

就是说,让我们使用泛型来解决原始类型问题:

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]));
   ...
   ...

此时,还有另一个问题,

语句LinkedList<Integer> arr2 = new LinkedList<>(Arrays.asList(array2)); 不会编译,因为array2的类型为int[]因此接收器的类型应为LinkedList<int[]>以便进行编译,但显然这不是您要的。

要解决该问题,您需要转换int[] 您可以按照以下步骤进行操作:

LinkedList<Integer> arr2 = Arrays.stream(array2)
                                 .boxed() 
                               .collect(Collectors.toCollection(LinkedList::new));

您正在ArrayList中使用泛型:

ArrayList<Integer> alist = new ArrayList<Integer>();

为什么不使用它:

LinkedList<Integer> arr2 = new LinkedList(Arrays.asList(array2));

Iterator<Integer> x = arr2.listIterator(arr2.indexOf(array1[0]));

代替

LinkedList arr2 = new LinkedList(Arrays.asList(array2));

Iterator x = arr2.listIterator(arr2.indexOf(array1[0]));

另一个没有泛型的解决方案(对您的情况没有用),您必须转换每个Object:

alist.add((Integer) x.next());
          ^^^^^^^^^^^^^^^^^^

alist.add((Integer) arr2.peek());
          ^^^^^^^^^

if(array1[i] != (Integer) arr2.get(i)){
                ^^^^^^^^^

另一种解决方案

正如@Aominè和其他人提到的那样,您必须解决一些问题,我只想用另一种方法解决您的问题:

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;
}

考虑您有这两个数组(我认为它们不为空且长度相同)

array1 = {4, 5, 6, 7, 1, 2}
array2 = {1, 2, 4, 5, 6, 7}

对于array1中的每个元素,将其移到末尾并检查它是否与not的第二个数组相等:

第一次迭代:

array1 = {4, 5, 6, 7, 1, 2}
array2 = {1, 2, 4, 5, 6, 7}

不相等

第二次迭代:

array1 = {4, 5, 6, 7, 1, 2}
array2 = {2, 4, 5, 6, 7, 1}//move 1 to the end

不相等

第二次迭代:

array1 = {4, 5, 6, 7, 1, 2}
array2 = {4, 5, 6, 7, 1, 2}//move 2 to the end

等于(和中断)

如果没有匹配项且迭代结束,则返回false表示不等于

因为您未指定Integer类型参数:

LinkedList<Integer> arr2 = new LinkedList<>(Arrays.asList(array2));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM