这里显然有3个问题,所以让我们一个一个地解决它们:
How exactly do I expect that list methods will work when I’m expecting an List of Integer and getting a List of int[] ?
那么,List方法将完全按照预期工作,List< T>是类型T的列表.这里T是一个int [],所以List< int []>将包含每个元素的数组:
[{1, 2}, {3, 4}, {1, 6}]
所以get(i)将返回第i个元素.在Arrays.asList的情况下,List包含一个元素,即int [],所以:
int[] array = {210,211,212};
List list = Arrays.asList(array);
将会
[{210, 211, 212}]
所以
list.get(0)[0] == 210
In case of Strings the return type is List of String and not List of String[]. What sort of implementation differences are there?
String是Object,而不是基本类型.差异来自于此.
What good is this method for primitives if things are so uncertain?
事情并不确定.此方法导致定义和可预测的行为.它对于原语来说并不是很有用.这是将Java类型系统与泛型相结合的(又一个)副作用.
请注意,Java 8将int []转换为List< Integer>很简单:
List list = Arrays.stream(array).
boxed().
collect(toList());