DEV Community

Cover image for [Tiny] How the conversion of int[] to List<Integer> can be buggy?
Petr Filaretov
Petr Filaretov

Posted on

8

[Tiny] How the conversion of int[] to List<Integer> can be buggy?

Imagine we have an array of ints and we want to convert it to a List. Here is the sample:

int[] array = new int[]{42, 5, 1, 3, 4};
List<Integer> list = new ArrayList(Arrays.asList(array));
System.out.println(list.get(0));
Enter fullscreen mode Exit fullscreen mode

What do you think will be printed? That's right, something like

[I@2ff4acd0
Enter fullscreen mode Exit fullscreen mode

It does not look like 42 at all. So, what's the problem?

First of all, let's look at the warnings IDE shows:

Raw use of parameterized class 'ArrayList'
Call to 'asList()' with only one argument
Enter fullscreen mode Exit fullscreen mode

Okay, so let's fix the first one by adding diamond operator <> to new ArrayList:

int[] array = new int[]{42, 5, 1, 3, 4};
List<Integer> list = new ArrayList<>(Arrays.asList(array));
System.out.println(list.get(0));
Enter fullscreen mode Exit fullscreen mode

And now we have a compilation error on the ArrayList creation:

Required type: List<Integer>
Provided: ArrayList<int[]>
Enter fullscreen mode Exit fullscreen mode

It looks like it matches the second warning we saw earlier. The reason is that Arrays.asList(array) does not box int to Integer and creates a List of <int[]>, not a List of Integer as we wanted it to.

There are several ways to fix it:

  • The type of array can be changed from int[] to Integer[]:
Integer[] array = new Integer[]{42, 5, 1, 3, 4};
List<Integer> list = new ArrayList<>(Arrays.asList(array));
System.out.println(list.get(0));
Enter fullscreen mode Exit fullscreen mode
  • array can be converted to List<Integer> using IntStream with boxing:
int[] array = new int[]{42, 5, 1, 3, 4};
List<Integer> list = new ArrayList<>(Arrays.stream(array).boxed().toList());
System.out.println(list.get(0));
Enter fullscreen mode Exit fullscreen mode
  • Another way to create IntStream from int[] is to use IntStream.of():
int[] array = new int[]{42, 5, 1, 3, 4};
List<Integer> list = new ArrayList<>(IntStream.of(array).boxed().toList());
System.out.println(list.get(0));
Enter fullscreen mode Exit fullscreen mode

All three fixes will work as expected, and the code will print 42.

As a conclusion, don't ignore IDE warnings, as they can indicate real bugs, not just code smells.


Dream your code, code your dream.

Speedy emails, satisfied customers

Postmark Image

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay