There are obviously 3 questions here, so they can solve them one at a time:
- How exactly do I expect list methods to work when I expect an Integer list and get an int [] list?
Well, List methods will work exactly as expected, List<T> is a list of T types. Here T is int[] , so a List<int[]> will contain arrays like every element:
[{1, 2}, {3, 4}, {1, 6}]
So get(i) will return the ith element. In the case of Arrays.asList List contains one element, namely int[] like this:
int[] array = {210,211,212}; List<int[]> list = Arrays.asList(array);
Will be
[{210, 211, 212}]
So,
list.get(0)[0] == 210
In the case of strings, the return type is List of String, not List of String []. What are the differences in implementation?
String is an Object , not a primitive type. Hence the difference.
- What good is this method for primitives if everything is so vague?
Things are not vague. This method leads to specific and predictable behavior. This is just not very useful for primitives. This is (another) side effect of combining a system like Java with generics.
Note that with Java 8, converting int[] to List<Integer> very simple:
List<Integer> list = Arrays.stream(array). boxed(). collect(toList());
Boris the Spider
source share