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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 02:24:37  来源:igfitidea点击:

Initialize ArrayList<Long>

javaarrayslistarraylistlong-integer

提问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 intto longif needed.

如果需要,Java 会自动转换intlong

However, Java does not do the sameif a transformation from Integerto Longis needed.

但是,如果需要从to转换,Java不会这样做IntegerLong

The function Arrays.asList(...)returns a List<E>with Ebeing the type used as parameters. As you use 1, 2, 3the type is int. However the generic usage of data-typesas List<int>is not possible in Java (at least currently). Therefore it automatically transforms intto Integerand 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中(至少目前)。因此它会自动转换intInteger并生成一个List<Integer>对象。这个过程称为自动装箱,Java 可以将所有数据类型都执行到它们对应的对象表示中。

If you now use the constructor new ArrayList<Integer>(List<E> list)it expects Eto 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 Eneeds to be of type Long. However the object Integeris not of typeLongthus it does not acceptthe parameter. The first common type of Integerand Longis the abstract class Number(which also holds Double, Floatand others) (documentation).

但是当你使用时new ArrayList<Long>(List<E> list)显然E需要是 type Long。然而,对象Integer类型的不Long因此它并不能接受的参数。Integerand的第一个常见类型Long是抽象类Number(也包含Double,Float和其他)(文档)。



Solution

解决方案

So it all revolves around the input 1, 2, 3being interpreted as intinstead of long. You can fix this by explicitlytelling Java to interpretthe numbers as long, you do so by appending lor Lafter the number:

所以这一切都围绕着输入1, 2, 3被解释为int而不是long. 您可以通过明确告诉 Java将数字解释为来解决此问题long,您可以通过附加lL在数字之后来实现:

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 floatinstead of double: 1.5For 1.5f

请注意,相同的技术可用于显式解释十进制数,float而不是double:1.5F1.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 1and 2are ints and Arrays.asList(1, 2)creates a List<Integer>.

那是因为1and2是整数并Arrays.asList(1, 2)创建了一个List<Integer>.

And the copy constructor of ArrayListrequires the argument to be of the same generic type.

并且 的复制构造函数ArrayList要求参数具有相同的泛型类型。

You have several options but the simplest one is to change the ints into longs by adding a Lsuffix:

您有多种选择,但最简单的一种是通过添加后缀将ints更改为longs 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 Longnumber using literal lor L.

您必须Long使用文字lL.

ArrayList<Long> x = new ArrayList<Long>(Arrays.asList(1L, 2L));