0

I have trouble understanding why the conversion to primitive array fails for a one dimensional array but works for the two dimensional case.

public static void main( String[] args ) throws IOException {
    List<Integer> oneD = List.of(1,2,3);
    int[] one = oneD.toArray(int[]::new); // error

    List<int[]> twoD = List.of(
            new int[]{1,2},
            new int[]{3,4}
            );

    int[][] two = twoD.toArray(int[][]::new); // works
}

I tried making my way through the documentation but of no avail. One thing I can see is that in the first case, since we are trying to convert from Integer to int, is what might be causing the error, and that the second case works fine since int[] would be an Object type.

An explanation with an example of how toArray works, especially for primitives would be really helpful.

2 Answers 2

3

Yup, Integer is nothing like int. An int[] is not an Object[].

Also, two dimensional arrays? You're mistaken; those do not exist. An int[][] is not a 2d-array of ints. It's a 1d array of int arrays. There is some light syntax sugar to let you write int[][] x = new int[5][10];, which is sugar for:

int[][] x = new int[5][];
for (int i = 0; i < x.length; i++) x[i] = new int[10];

and that's nice and convenient, but under the hood it's not a 2D array; these do not exist. The 'component type' of x.getClass() is int[].class - and then array can be 'disjointed' (you can have row 0 contain 5 cells, and row 1 contain 10 cells, i.e. you could write x[1] = new int[20];` and this works fine.

That explains why the twoD example works.

To convert a List<Integer> to an int[], write a for loop.

List<Integer> list = ...;
int[] arr = new int[list.size()];
for (int i = 0; i < list.size(); i++) arr[i] = list.get(i);
Sign up to request clarification or add additional context in comments.

3 Comments

I am aware of how arrays work. Perhaps my lack in understanding arises from confusion around Objects and reference type(int[] here). I assumed that Collections only work for Object types and hence I said that int[] would be an Object. .Can you help clarify a bit around why the twoD example works
Because an int[] is an Object (Object x = new int[10]; is legal java code), but an int is not (Object x = 5; does 'work', but only because that 5 is silently converted (autoboxed)). .toArray() can only convert to an array of some object type. Thus, int[][] is fine (that is an array of int[], and int[] is an object), but int[] is not (that is an array of int which is not an object type).
Yes. This is what my presumption was, which is why I stated int[] as an object. I misread your answer wherein you stated int[] is not Object[] as Object. Thanks for your explanation, marked as accepted.
1

toArray cannot generate primitive arrays since Lists can't contain primitive elements.

You can use IntStream to achieve the conversion without an explicit loop:

int[] one = oneD.stream ().mapToInt (Integer::intValue).toArray ();

2 Comments

Thanks for this. I had researched well into converting two and fro between primitives and collections. But understanding how it interprets int[][] was my point of confusion. Taking int[] as T and then writing T[] makes it a bit clear now why second example works. Still unsure why int[] is not an Object. I thought Java only has Primitives and Objects, wherein Objects are just references themselves
@arjunkhera int[] is an object. Its elements are not. Since int[] is an Object, int[][] also is an Object[] which is the reason why toArray works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.