问题描述
我编写了以下代码并惊讶地看到输出:
整数 a = 211;int b = 211;int[] 数组 = {210,211,212};System.out.println(Arrays.asList(array).contains(a));System.out.println(Arrays.asList(array).contains(b));
输出:
false错误的
我发现了这个问题和其他一些链接的问题到它并了解到 asList
方法不会自动装箱.我在 eclipse javadoc preview 中检查了返回的类型:
我不太理解这种返回类型.int[]
是一个对象而不是一个原语,所以它很好.我确定我没有得到 List
(我期望的东西),但我不确定如何使用返回的东西.我的问题是:
- 1.当我期待一个 Integer 列表并获得一个 int[] 列表时,我究竟期望列表方法如何工作?
- 2.在字符串的情况下,返回类型是字符串列表而不是字符串列表[].有哪些实现差异?
- 3.如果事情如此不确定,这种方法对原语有什么好处?
这里显然有 3 个问题,让我们一一解决:
- 当我期待一个整数列表并获得一个 int[] 列表时,我究竟希望列表方法如何工作?
好吧,List
方法将完全按预期工作,List
是类型 T
的列表.这里 T
是一个 int[]
,所以 List
将包含数组作为每个元素:
[{1, 2}, {3, 4}, {1, 6}]
所以 get(i)
将返回 ith 元素.对于
Arrays.asList
,List
包含一个元素,即 int[]
所以:
int[] 数组 = {210,211,212};列表list = Arrays.asList(array);
会
[{210, 211, 212}]
所以
list.get(0)[0] == 210
在字符串的情况下,返回类型是字符串列表而不是字符串列表[].有哪些实现差异?
String
是一个 Object
,不是原始类型.区别由此而来.
- 如果事情如此不确定,这种方法对原语有什么好处?
事情并非不确定.此方法导致定义和可预测行为.它只是对基元不是很有用.这是结合 Java 的类型系统和泛型的(又一个)副作用.
请注意,在 Java 8 中,int[]
到 List
的转换非常简单:
List列表 = Arrays.stream(array).装箱().收集(toList());
I wrote following code and was surprised to see the output:
Integer a = 211;
int b = 211;
int[] array = {210,211,212};
System.out.println(Arrays.asList(array).contains(a));
System.out.println(Arrays.asList(array).contains(b));
Output:
false
false
I found this question and some other questions linked to it and learned that asList
method doesn't Autobox stuffs. I checked the returned type in eclipse javadoc preview:
I couldn't quite understand this return type. int[]
is an object and not a primitive so its fine. I'm sure that I'm not getting List<Integer>
(something which I expected) but I'm not sure how to use the thing which is being returned. My questions are:
- 1. How exactly do I expect that list methods will work when I'm expecting an List of Integer and getting a List of int[] ?
- 2. In case of Strings the return type is List of String and not List of String[]. What sort of implementation differences are there?
- 3. What good is this method for primitives if things are so uncertain?
There are obviously 3 questions here so lets tackle them one by one:
Well, List
methods will work exactly as expected, a List<T>
is a list of types T
. Here T
is an int[]
so a List<int[]>
will contains arrays as each element:
[{1, 2}, {3, 4}, {1, 6}]
So get(i)
will return the ith
element. In the case of Arrays.asList
the List
contains a single element, namely the int[]
so:
int[] array = {210,211,212};
List<int[]> list = Arrays.asList(array);
Will be
[{210, 211, 212}]
And so
list.get(0)[0] == 210
String
is an Object
, not a primitive type. The difference follows from that.
Things are not uncertain. This method results in defined and predictable behaviour. It's just not very useful for primitives. This is (yet another) side effect of combining Java's type system with generics.
Note with Java 8 the conversion of an int[]
to a List<Integer>
is very simple:
List<Integer> list = Arrays.stream(array).
boxed().
collect(toList());
这篇关于java:了解基本类型的 Arrays.asList(T...array) 方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!