This question already has answers here:
What is a raw type and why shouldn't we use it?
                            
                                (15个答案)
                            
                    
                2年前关闭。
        

    

这是来自谷歌foo bar电源饥饿挑战的代码。我收到一个错误。我如何找出问题所在?

错误:


  Answer.java:36:错误:无法比较的类型:对象和整数
              如果(intList.get(i)== 0 || intList.get(i)== 1){
                                 ^ Answer.java:36:错误:无与伦比的类型:对象和整数
              如果(intList.get(i)== 0 || intList.get(i)== 1){
                                                        ^ Answer.java:37:错误:无与伦比的类型:对象和整数
                  if(intList.get(i)== 1){oneExists = true;}
                                     ^ Answer.java:49:错误:二进制运算符“           如果(intList.size()== 1 && intList.get(0)                                                   ^第一种类型:对象第二种类型:int Answer.java:57:错误:不兼容的类型:
  无法将对象转换为整数
          对于(Integer i:intList){
                           ^ Answer.java:68:错误:不兼容的类型:无法将对象转换为Integer
          对于(Integer i:intList){
                           ^注意:Answer.java使用未经检查或不安全的操作。注意:使用-Xlint:unchecked重新编译以获取详细信息。 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();
    }
}

最佳答案

intList被定义为原始的List

List intList = new ArrayList();


因此intList.get(i)返回一个Object,它不能与int进行比较。

更改为

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


为了使intList.get(i)返回Integer,可以将其与int进行比较。

关于java - 出现不可比类型的错误:Object和int ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48849115/

10-12 13:30