简体   繁体   中英

error: incompatible types: int[] cannot be converted to Integer[]

This is my code for a problem that I am trying to solve

public static int totalchocolates(Integer[] input1) {

    int countEaten = 0;
    Arrays.sort(input1, Collections.reverseOrder());

    for (int i = input1.length - 1; i > -1; i--) {
        countEaten = (int)(countEaten + Math.ceil(input1[i].doubleValue() / 2));
        if (i > 1 && (input1[i - 1] + input1[i] / 2) > 1000) {
            i = i - 1;
        }
    }
    return countEaten;
}

The main fuction is

public static void main(String[] args) throws IOException {
    Scanner in = new Scanner(System.in);
    int output = 0;
    int ip1_size = 0;
    ip1_size = Integer.parseInt(in.nextLine().trim());
    int[] ip1 = new int[ip1_size];
    int ip1_item;
    for (int ip1_i = 0; ip1_i < ip1_size; ip1_i++) {
        ip1_item = Integer.parseInt( in .nextLine().trim());
        ip1[ip1_i] = ip1_item;
    }
    output = totalchocolates(ip1);
    System.out.println(String.valueOf(output));
}

I am getting the following error,

CandidateCode.java:36: error: incompatible types: int[] cannot be converted to >Integer[] output = totalchocolates(ip1);

Error clearly says that you are providing int[] as input parameter where as your function is expecting Integer[] array. It would be better you change the input array to type Integer[]

Integer[] ip1 = new Integer[ip1_size];

In Java, Integer and int refer to different types - Integer wraps int in a object type, and provides several utility methods. As a result, these arrays are incompatible. You will need both to be either int[] or Integer[]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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