Resolving Java Type Mismatch Error: Cannot Convert from List<int[]> to List<Integer>

  • Last updated Apr 25, 2024

The error "Type mismatch: cannot convert from List to List " arises when attempting to convert a List of Integer to an int array. The code snippet below demonstrates this error:

int[] intArray = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
//Type mismatch: cannot convert from List to List
List integerList = Arrays.asList(intArray);

To fix this error, you need to convert the int arrays to Integer objects and create a List of Integer. Here's an example of how you can resolve this issue:

// sample list of int items
int[] intArray = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };

// converting array of int into list of int
List intList = new ArrayList();

for(int n : intArray) {
    intList.add(n);
}