获得无法比较的类型的错误:Object和int

时间:2018-02-18 06:36:11

标签: java arrays

这是来自google foo bar强力挑战的代码。我收到了一个错误。我该如何找出问题?

错误:

  

Answer.java:36:错误:无法比拟的类型:Object和int               if(intList.get(i)== 0 || intList.get(i)== 1){                                  ^ Answer.java:36:错误:无法比拟的类型:Object和int               if(intList.get(i)== 0 || intList.get(i)== 1){                                                         ^ Answer.java:37:错误:无与伦比的类型:对象和int                   if(intList.get(i)== 1){oneExists = true;}                                      ^ Answer.java:49:错误:二元运算符'<'的错误操作数类型           if(intList.size()== 1&& intList.get(0)< 0){                                                    ^第一种类型:对象第二种类型:int Answer.java:57:错误:不兼容的类型:   对象无法转换为Integer           for(Integer i:intList){                            ^ Answer.java:68:错误:不兼容的类型:对象无法转换为Integer           for(Integer i:intList){                            ^注意:Answer.java使用未经检查或不安全的操作。注意:使用-Xlint重新编译:取消选中以获取详细信息。 6   错误

package foobar.PowerHungry;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;


public class Answer {
    public static String answer(int[] xs) {
        List intList = new ArrayList();
        BigInteger resultNumber = new BigInteger("1");// the result number might be huge which might not fit inside an Integer;

        //lets convert the int[] to a list of integers so its easier to work with (You don't have to do this, this is just personal preference)
        for (int x : xs) {
            intList.add(x);
        }

        //if there is only one element just return it.
        if (intList.size() == 1){
            return intList.get(0).toString();
        }

        //Next Lets remove all 0's from the list as anything * 0 = 0 && anything * 1 = itself
        boolean oneExists = false;
        for (int i = 0; i<intList.size(); i++) {
            if (intList.get(i) == 0 || intList.get(i) == 1) {
                if (intList.get(i) == 1){oneExists = true;}
                intList.remove(i);
                //we just popped out an element in the so we need to go back as to not skip the moved down element
                i--;
            }
        }
        //if the array is empty check if there was ever a 1 and return the result
        if (intList.size() == 0){
            if (oneExists){return "1";}
            else {return "0";}
        }
        //after removing all the 0's and 1's, if there is one element and its negative, lets return 0 as turning off the panel is better than it draining energy
        if (intList.size() == 1 && intList.get(0)< 0){
            return "0";
        }

        //Lets check how many negative numbers are in the array, if there is an odd number then lets remove the negative number closest to 0
        // to create a large subset that comes out to a positive number
        Integer negativeCount = 0;
        Integer smallestNegative = Integer.MIN_VALUE;
        for (Integer i : intList) {
            if (i<0) { negativeCount++; if (i<smallestNegative) {
                    smallestNegative = i;
                }
            }
        }
        //Now if the number of negatives is odd remove the smallest negative
        if (negativeCount % 2 == 1) {
            intList.remove(smallestNegative);
        }
        //now with an even number of negatives and positives, lets multiply all the elements together to get the highest number
        for (Integer i : intList) {
            resultNumber = resultNumber.multiply(new BigInteger(i.toString()));
        }

        //Return the result as a string
        return resultNumber.toString();
    }
}

1 个答案:

答案 0 :(得分:1)

intList被定义为原始List

List intList = new ArrayList();

因此intList.get(i)会返回Object,无法将其与int进行比较。

将其更改为

List<Integer> intList = new ArrayList<>();

intList.get(i)返回Integer,可以与int进行比较。