Java 创建一个原始int列表?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18021218/
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
Create a List of primitive int?
提问by Susie
Is there a way to create a list of primitive int or any primitives in java like following?
有没有办法在java中创建原始int或任何原始类型的列表,如下所示?
List<int> myList = new ArrayList<int>();
It seems I can do List myList = new ArrayList();
看来我可以 List myList = new ArrayList();
and add "int" into this list. But then this would mean I can add anything into this list.
并将“int”添加到此列表中。但这意味着我可以在此列表中添加任何内容。
Is my only option, creating an array of int and converting it into a list or creating a list of Integer objects?
我唯一的选择是创建一个 int 数组并将其转换为一个列表还是创建一个 Integer 对象列表?
采纳答案by Kevin Bowersox
In Java the type of any variable is either a primitive type or a reference type. Generic type arguments must be reference types. Since primitives do not extend Object
they cannot be used as generic type arguments for a parametrized type.
在 Java 中,任何变量的类型要么是原始类型,要么是引用类型。泛型类型参数必须是引用类型。由于原语不扩展,Object
它们不能用作参数化类型的泛型参数。
Instead use the Integer
class which is a wrapper for int
:
而是使用Integer
作为包装器的类int
:
List<Integer> list = new ArrayList<Integer>();
If your using Java 7 you can simplify this declaration using the diamond operator:
如果您使用 Java 7,您可以使用菱形运算符简化此声明:
List<Integer> list = new ArrayList<>();
With autoboxing in Java the primitive type int
will become an Integer
when necessary.
使用 Java 中的自动装箱,原始类型int
将Integer
在必要时变为一个。
Autoboxing is the automatic conversion that the Java compiler makes between the primitive types and their corresponding object wrapper classes.
自动装箱是 Java 编译器在原始类型与其对应的对象包装类之间进行的自动转换。
So the following is valid:
所以以下是有效的:
int myInt = 1;
List<Integer> list = new ArrayList<Integer>();
list.add(myInt);
System.out.println(list.get(0)); //prints 1
回答by nanofarad
This is not possible. The java specification forbids the use of primitives in generics. However, you can create ArrayList<Integer>
and call add(i)
if i
is an int thanks to boxing.
这不可能。java 规范禁止在泛型中使用原语。但是,由于拳击,您可以创建ArrayList<Integer>
和调用add(i)
ifi
是一个 int 。
回答by Reimeus
Collections use generics which support either reference typesor wilcards. You can however use an Integer
wrapper
集合使用支持引用类型或通配符的泛型。但是,您可以使用Integer
包装器
List<Integer> list = new ArrayList<>();
回答by Rohit Jain
Is there a way to create a list of primitive int or any primitives in java
有没有办法在java中创建原始int或任何原始类型的列表
No you can't. You can only create List of reference types, like Integer
, String
, or your custom type.
不,你不能。您只能创建引用类型,喜欢的列表Integer
,String
或您的自定义类型。
It seems I can do
List myList = new ArrayList();
and add "int" into this list.
看来我可以这样做
List myList = new ArrayList();
并将“int”添加到此列表中。
When you add int
to this list, it is automatically boxed to Integer
wrapper type. But it is a bad idea to use rawtype lists, or for any generic type for that matter, in newer code.
当您添加int
到此列表时,它会自动装箱为Integer
包装器类型。但是在较新的代码中使用原始类型列表或任何泛型类型是一个坏主意。
I can add anything into this list.
我可以在此列表中添加任何内容。
Of course, that is the dis-advantage of using raw type. You can have Cat, Dog, Tiger, Dinosaur, all in one container.
当然,这是使用原始类型的缺点。您可以将Cat、Dog、Tiger、Dinosaur全部放在一个容器中。
Is my only option, creating an array of int and converting it into a list
是我唯一的选择,创建一个 int 数组并将其转换为一个列表
In that case also, you will get a List<Integer>
only. There is no way you can create List<int>
or any primitives.
在这种情况下,你也会得到一个List<Integer>
。您无法创建List<int>
或任何基元。
You shouldn't be bothered anyways. Even in List<Integer>
you can add an int
primitive types. It will be automatically boxed, as in below example:
反正你不应该被打扰。即使在List<Integer>
您可以添加int
原始类型。它将自动装箱,如下例所示:
List<Integer> list = new ArrayList<Integer>();
list.add(5);
回答by Mike B
No there isn't any collection that can contain primitive types when Java Collection Framework is being used.
不,当使用 Java Collection Framework 时,没有任何可以包含原始类型的集合。
However, there are other java collections which support primitive types, such as: Trove, Colt, Fastutil, Guava
但是,还有其他支持原始类型的 Java 集合,例如:Trove、Colt、Fastutil、Guava
An example of how an arraylist with ints would be when Trove Library used is the following:
当使用 Trove 库时,带有整数的数组列表的示例如下:
TIntArrayList list= new TIntArrayList();
The performance of this list, when compared with the ArrayList of Integers from Java Collections is much better as the autoboxing/unboxing to the corresponding Integer Wrapper Class is not needed.
与 Java 集合中的整数 ArrayList 相比,此列表的性能要好得多,因为不需要对相应的整数包装类进行自动装箱/拆箱。
回答by Selvaraj
Java Collection should be collections of Object only.
Java Collection 应该只是 Object 的集合。
List<Integer> integerList = new ArrayList<Integer>();
Integer is wrapper class of primitive data type int.
Integer 是原始数据类型 int 的包装类。
more from JAVA wrapper classes here!
更多来自这里的JAVA 包装器类 !
U can directly save and get int to/from integerList as,
integerList.add(intValue);
int intValue = integerList.get(i)
你可以直接保存和获取 int 到/从 integerList 为,
integerList.add(intValue);
int intValue = integerList.get(i)
回答by stolen_leaves
Try using the ArrayIntList from the apache framework. It works exactly like an arraylist, except it can hold primitive int.
尝试使用 apache 框架中的 ArrayIntList。它的工作方式与 arraylist 完全一样,只是它可以保存原始 int。
More details here -
更多细节在这里 -
回答by Robin Davies
Is there a way to convert an Integer[] array to an int[] array?
有没有办法将 Integer[] 数组转换为 int[] 数组?
This gross omission from the Java core libraries seems to come up on pretty much every project I ever work on. And as convenient as the Trove library might be, I am unable to parse the precise requirements to meet LPGL for an Android app that statically links an LGPL library (preamble says ok, body does not seem to say the same). And it's just plain inconvenient to go rip-and-stripping Apache sources to get these classes. There has to be a better way.
Java 核心库的这种严重遗漏似乎出现在我参与过的几乎每个项目中。尽管 Trove 库可能很方便,但对于静态链接 LGPL 库的 Android 应用程序,我无法解析满足 LPGL 的精确要求(序言说好的,正文似乎不一样)。而且为了获得这些类而去撕掉 Apache 源代码是很不方便的。一定有更好的方法。
回答by Stefan Medack
When you use Java for Android development, it is recommended to use SparseIntArray
to prevent autoboxing between int
and Integer
.
使用Java进行Android开发时,建议使用SparseIntArray
防止int
和Integer
.
You can finde more information to SparseIntArray
in the Android Developers documentationand a good explanation for autoboxing on Android enter link description here
您可以SparseIntArray
在Android 开发人员文档中找到更多信息,并在此处对 Android 上的自动装箱进行了很好的解释,在此处输入链接描述
回答by Nikhil Nanivadekar
You can use primitive collections available in Eclipse Collections. Eclipse Collections has List
, Set
, Bag
and Map
for all primitives. The elements in the primitive collections are maintained as primitives and no boxing takes place.
您可以使用Eclipse Collections 中提供的原始集合。Eclipse Collections 具有List
, Set
,Bag
和Map
用于所有原语。原始集合中的元素作为原始元素进行维护,并且不会发生装箱。
You can initialize a IntListlike this:
您可以像这样初始化一个IntList:
MutableIntList ints = IntLists.mutable.empty();
You can convert from a List<Integer>
to IntList
like this:
您可以从一个转换List<Integer>
到IntList
这样的:
List<Integer> integers = new ArrayList<>();
MutableIntList ints = ListAdapter.adapt(integers).collectInt(each -> each);
Note: I am a contributor to Eclipse Collections.
注意:我是 Eclipse Collections 的贡献者。