0

Arrays.asList returns List of int[] when an int array is passed however it returns List of String when String[] is passed and not List of String[]

int ai[]={1,2};
List<int[]> aiList=Arrays.asList(ai);

same is not true for Strings ..Any reason

String[] str={"a","b"};  //string array
List<String> strlist=Arrays.asList(str); //valid statement

Below is not valid
String[] str={"a","b"};
List<String[]> strlist=Arrays.asList(str);
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
sks
  • 1
  • https://stackoverflow.com/questions/31422025/arrays-aslistint-not-working – Eran Sep 06 '17 at 06:06
  • 1
    your arrays is made of primitives that is the reason – ΦXocę 웃 Пepeúpa ツ Sep 06 '17 at 06:10
  • The reason for this behavior is because String is not a primitive type in Java. Same is the case with `Integers`. Following code is valid: `Integer[] ints = new Integer[] {1,2,3,4,5}; List list = Arrays.asList(ints);` But this is not: `Integer[] ints = new Integer[] {1,2,3,4,5}; List list = Arrays.asList(ints);` – Chinmay jain Sep 06 '17 at 06:21

1 Answers1

0

In the second example you are trying to create a list with each element in it is an array of Strings. This is not matching the type on the right side is is a list of Strings.