Java 初始化 ArrayList<Long>
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46564897/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Initialize ArrayList<Long>
提问by Luke
Why I can initialize ArrayList, like this:
为什么我可以像这样初始化 ArrayList:
ArrayList<Integer> x = new ArrayList<Integer>(Arrays.asList(1,2));
But got Error when using:
但是在使用时出现错误:
ArrayList<Long> x = new ArrayList<Long>(Arrays.asList(1,2));
采纳答案by Zabuzard
Explanation
解释
Java automatically transforms int
to long
if needed.
如果需要,Java 会自动转换int
为long
。
However, Java does not do the sameif a transformation from Integer
to Long
is needed.
但是,如果需要从to转换,Java不会这样做。Integer
Long
The function Arrays.asList(...)
returns a List<E>
with E
being the type used as parameters. As you use 1, 2, 3
the type is int
. However the generic usage of data-typesas List<int>
is not possible in Java (at least currently). Therefore it automatically transforms int
to Integer
and produces a List<Integer>
object. This process is called auto-boxing, Java can do this for all data-types to their corresponding object representation.
该函数Arrays.asList(...)
返回一个List<E>
与E
被用作参数的类型。当您使用1, 2, 3
类型是int
. 然而一般使用的数据类型为List<int>
是不可能在Java中(至少目前)。因此它会自动转换int
为Integer
并生成一个List<Integer>
对象。这个过程称为自动装箱,Java 可以将所有数据类型都执行到它们对应的对象表示中。
If you now use the constructor new ArrayList<Integer>(List<E> list)
it expects E
to be something of type Integer
. So a List<Integer>
works as input.
如果你现在使用构造函数,new ArrayList<Integer>(List<E> list)
它E
应该是 type 的东西Integer
。所以 aList<Integer>
可以作为输入。
But when you use new ArrayList<Long>(List<E> list)
obviously E
needs to be of type Long
. However the object Integer
is not of typeLong
thus it does not acceptthe parameter. The first common type of Integer
and Long
is the abstract class Number
(which also holds Double
, Float
and others) (documentation).
但是当你使用时new ArrayList<Long>(List<E> list)
显然E
需要是 type Long
。然而,对象Integer
是类型的不Long
因此它并不能接受的参数。Integer
and的第一个常见类型Long
是抽象类Number
(也包含Double
,Float
和其他)(文档)。
Solution
解决方案
So it all revolves around the input 1, 2, 3
being interpreted as int
instead of long
. You can fix this by explicitlytelling Java to interpretthe numbers as long
, you do so by appending l
or L
after the number:
所以这一切都围绕着输入1, 2, 3
被解释为int
而不是long
. 您可以通过明确告诉 Java将数字解释为来解决此问题long
,您可以通过附加l
或L
在数字之后来实现:
new ArrayList<Long>(Arrays.asList(1L, 2L, 3L));
Now you receive a List<Long>
which then is added to an ArrayList<Long>
.
现在您会收到一个List<Long>
,然后将其添加到ArrayList<Long>
.
Note that the same technique can be used to explicitly interpret decimal numbers as float
instead of double
: 1.5F
or 1.5f
请注意,相同的技术可用于显式解释十进制数,float
而不是double
:1.5F
或1.5f
回答by Mena
Because Arrays.asList(1,2)
will implicitly return a List<Integer>
.
因为Arrays.asList(1,2)
会隐式返回一个List<Integer>
.
You can fix this by using the following idiom:
您可以使用以下习语来解决此问题:
ArrayList<Long> x = new ArrayList<Long>(Arrays.asList(1l,2l));
回答by assylias
That's because 1
and 2
are ints and Arrays.asList(1, 2)
creates a List<Integer>
.
那是因为1
and2
是整数并Arrays.asList(1, 2)
创建了一个List<Integer>
.
And the copy constructor of ArrayList
requires the argument to be of the same generic type.
并且 的复制构造函数ArrayList
要求参数具有相同的泛型类型。
You have several options but the simplest one is to change the int
s into long
s by adding a L
suffix:
您有多种选择,但最简单的一种是通过添加后缀将int
s更改为long
s L
:
List<Long> x = new ArrayList<Long>(Arrays.asList(1L, 2L));
Note that with Java 9 you can also write:
请注意,使用 Java 9,您还可以编写:
List<Long> x = List.of(1L, 2L);
回答by Nikolas
You have to specify a Long
number using literal l
or L
.
您必须Long
使用文字l
或L
.
ArrayList<Long> x = new ArrayList<Long>(Arrays.asList(1L, 2L));