0

Here is a code from google foo bar power hungry challenge. I am getting an error. How can I figure out the problem?

Error:

Answer.java:36: error: incomparable types: Object and int if (intList.get(i) == 0 || intList.get(i) == 1) { ^ Answer.java:36: error: incomparable types: Object and int if (intList.get(i) == 0 || intList.get(i) == 1) { ^ Answer.java:37: error: incomparable types: Object and int if (intList.get(i) == 1){oneExists = true;} ^ Answer.java:49: error: bad operand types for binary operator '<' if (intList.size() == 1 && intList.get(0)< 0){ ^ first type: Object second type: int Answer.java:57: error: incompatible types: Object cannot be converted to Integer for (Integer i : intList) { ^ Answer.java:68: error: incompatible types: Object cannot be converted to Integer for (Integer i : intList) { ^ Note: Answer.java uses unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details. 6 errors

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 &amp;&amp; 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();
    }
}
halfer
  • 19,824
  • 17
  • 99
  • 186

1 Answers1

1

intList is defined as a raw List:

List intList = new ArrayList();

So intList.get(i) returns an Object, which cannot be compared to an int.

Change it to

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

in order for intList.get(i) to return an Integer, which can be compared to an int.

Eran
  • 387,369
  • 54
  • 702
  • 768