0

So I am trying to convert an (primitive) int array to a List,

            for(int i=0;i<limit;i++) {
                arr[i]=sc.nextInt();
            }
            List list=Arrays.asList(arr);
            System.out.print(list);

Doing this so prints values like this for example, [[I@59a6e353] . There is an option to add <Integer> before variable list but since the array is primitive int, it could not. Is there any solution for this? Adding <int[]> before variable list isn't an option as I need to call a bounded wildcard method.

Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
Guhan
  • 31
  • 1
  • 7
  • don't print the list, print the content of the list. Lists don't have an implementation for toString, so your result is to be expected – Stultuske May 17 '22 at 06:55
  • Use `System.out.print(Arrays.toString(list))` – Jhanzaib Humayun May 17 '22 at 06:56
  • @Stultuske printing the contents of the list gives the same result too. – Guhan May 17 '22 at 06:57
  • @Rakkun in that case, how did you try to print the content? – Stultuske May 17 '22 at 06:59
  • @Stultuske for(Object i:list) { System.out.println(i); } – Guhan May 17 '22 at 07:01
  • ` List java.util.Arrays.asList(int[]... a)` you convert the array to a list of `int[]` – XtremeBaumer May 17 '22 at 07:03
  • @JhanzaibHumayun tried that but the ide warns like, The method toString(long[]) in the type Arrays is not applicable for the arguments (List), even though there is a same method for object[] too. – Guhan May 17 '22 at 07:04
  • @Rakkun that's because you force it to use the toString implementation of the Object class, why so surprised it uses the toString implementation of the Object class? – Stultuske May 17 '22 at 07:05
  • @XtremeBaumer that is an option but I need to call a (bounded) wildcard method, which I cant call if I try to add – Guhan May 17 '22 at 07:05
  • The issue is that you have an array of primitive type `int`. `Arrays.asList()` can't convert it to a `List` (`List` is not valid). You need to either have an `Integer[]` array or use another method to convert `int[]` to `List` – XtremeBaumer May 17 '22 at 07:08
  • @XtremeBaumer yea, that solved my question. – Guhan May 17 '22 at 07:10
  • 1
    if you already have an `int[]` array: `List list = IntStream.of(array).boxed().toList()` - BTW it is recommended not to use raw types! [JLS 4.8](https://docs.oracle.com/javase/specs/jls/se18/html/jls-4.html#jls-4.8-400): "*The use of raw types is allowed only as a concession to compatibility of legacy code. The use of raw types in code written after the introduction of generics into the Java programming language is strongly discouraged. It is possible that future versions of the Java programming language will disallow the use of raw types.*" – user16320675 May 17 '22 at 07:22
  • @user16320675 yes, seems like Java really discourages the use of raw primitive types in generics. – Guhan May 17 '22 at 07:41

2 Answers2

1

Arrays.asList(arr) does not creates a List<Integer> but a List<int[]> with a single element (your arr). You have to declare your variable as Integer[] arr if you have to use 'Arrays.asList'.

Stefan Warminski
  • 1,845
  • 1
  • 9
  • 18
0

The signature of method Arrays.asList is the following one:

public static <T> List<T> asList(T... a);

You can see that it needs an array of generics T in input.

Therefore you can pass an array of Integer and it works. Example:

List<Integer> list= Arrays.asList(new Integer[] {1,2,3,4});
System.out.print(list);

Output:

[1, 2, 3, 4]

If you pass int[] you can have just a List<int[]> because int is a primitive type and it cannot be used as generic. Example:

List<int[]> list= Arrays.asList(new int[] {1,2,3,4});
System.out.print(list);

Output:

[[I@6b1274d2]

I suggest you to read the answer in this question.

frascu
  • 747
  • 5
  • 9