java的:基本类型谅解Arrays.asList(T ...数组)方法 [英] java : Understanding Arrays.asList(T...array) method for primitive types
问题描述
我写以下code和惊讶地看到输出:
整数= 211;
INT B = 211; INT []数组= {} 210211212; 的System.out.println(Arrays.asList(阵列)。载有(一));
的System.out.println(Arrays.asList(阵列)。载有(B));
输出:
假
假
我发现<一个href=\"http://stackoverflow.com/questions/1073919/how-to-convert-int-into-listinteger-in-java\">this问题链接到它,得知 asList
方法不Autobox东西一些其他问题。我检查了的Eclipse Javadoc preVIEW返回类型:
我也不太明白返回类型。 INT []
是一个对象,而不是原始所以它的罚款。我敢肯定,我没有收到列表&LT;整数GT;
(这一点我预期),但我不知道如何使用它返回的东西。我的问题是:
- 1。究竟如何我希望清单方法将工作时,我期待整数的名单并获得INT []的列表?
- 2。在字符串的情况下,返回类型为String的列表,而不是字符串[]列表。什么样的实现差异有哪些?
- 3。有什么好处是这种方法的原语,如果事情是如此不确定?
有明显的3个问题在这里所以让我们解决这些问题一个接一个:
- 究竟如何我希望清单方法将工作时,我期待整数的名单并获得INT []?
的List
块引用>好吧,
列表
方法,将工作完全符合市场预期,一个列表&LT; T&GT;
是一个类型列表T
。在这里,T
是<为code> INT [] 所以列表&LT; INT []&GT;
将包含数组作为每个元素:[{1,2},{3,4},{1,6}]
所以
GET(我)
将返回第i
元素。在的情况下,Arrays.asList
的列表
包含一个元素,即INT [ ]
这样:INT []数组= {} 210211212;
清单&LT; INT [] GT;清单= Arrays.asList(数组);为
[{210,211,212}]
所以
list.get(0)[0] == 210
在字符串的情况下,返回类型为String的列表和字符串不是列表[]。什么样的实现差异有哪些?
块引用>
字符串
是对象
,不可以基本类型。所不同的遵循从。
<醇开始=3>
有什么好处是这种方法的原语,如果事情是如此不确定?
块引用>事情没有的不确定的。这种方法导致的确定和 predictable 的行为。它只是不适合原语非常有用的。这是(另一个)Java的类型系统的仿制药相结合的副作用。
请注意与Java 8转换的
INT []
到列表&LT;整数GT;
很简单:列表&LT;整数GT;清单= Arrays.stream(阵列)。
盒装()。
收集(了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 gettingList<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:
- How exactly do I expect that list methods will work when I'm expecting an List of Integer and getting a List of int[] ?
Well,
List
methods will work exactly as expected, aList<T>
is a list of typesT
. HereT
is anint[]
so aList<int[]>
will contains arrays as each element:[{1, 2}, {3, 4}, {1, 6}]
So
get(i)
will return theith
element. In the case ofArrays.asList
theList
contains a single element, namely theint[]
so:int[] array = {210,211,212}; List<int[]> list = Arrays.asList(array);
Will be
[{210, 211, 212}]
And so
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
is anObject
, not a primitive type. The difference follows from that.
- What good is this method for primitives if things are so uncertain?
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 aList<Integer>
is very simple:List<Integer> list = Arrays.stream(array). boxed(). collect(toList());
这篇关于java的:基本类型谅解Arrays.asList(T ...数组)方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!