[英]How to create ArrayList (ArrayList<Integer>) from array (int[]) in Java
I have seen the question: Create ArrayList from array 我看到了一个问题: 从数组中创建ArrayList
However when I try that solution with following code, it doesn't quite work in all the cases: 但是,当我使用以下代码尝试该解决方案时,它并不适用于所有情况:
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
public class ToArrayList {
public static void main(String[] args) {
// this works
String[] elements = new String[] { "Ryan", "Julie", "Bob" };
List<String> list = new ArrayList<String>(Arrays.asList(elements));
System.out.println(list);
// this works
List<Integer> intList = null;
intList = Arrays.asList(3, 5);
System.out.println(intList);
int[] intArray = new int[] { 0, 1 };
// this doesn't work!
intList = new ArrayList<Integer>(Arrays.asList(intArray));
System.out.println(intList);
}
}
What am I doing wrong here? 我在这做错了什么? Shouldn't the code
intList = new ArrayList<Integer>(Arrays.asList(intArray));
代码不应该是
intList = new ArrayList<Integer>(Arrays.asList(intArray));
compile just fine? 编译得好吗?
The problem in 问题在于
intList = new ArrayList<Integer>(Arrays.asList(intArray));
is that int[]
is considered as a single Object
instance since a primitive array extends from Object
. 是因为原始数组从
Object
扩展,因此int[]
被视为单个Object
实例。 This would work if you have Integer[]
instead of int[]
since now you're sending an array of Object
. 如果您使用
Integer[]
而不是int[]
这将起作用,因为现在您正在发送一个Object
数组。
Integer[] intArray = new Integer[] { 0, 1 };
//now you're sending a Object array
intList = new ArrayList<Integer>(Arrays.asList(intArray));
From your comment: if you want to still use an int[]
(or another primitive type array) as main data, then you need to create an additional array with the wrapper class. 从您的注释:如果您仍想使用
int[]
(或其他基本类型数组)作为主数据,那么您需要使用包装类创建一个额外的数组。 For this example: 对于这个例子:
int[] intArray = new int[] { 0, 1 };
Integer[] integerArray = new Integer[intArray.length];
int i = 0;
for(int intValue : intArray) {
integerArray[i++] = intValue;
}
intList = new ArrayList<Integer>(Arrays.asList(integerArray));
But since you're already using a for
loop, I wouldn't mind using a temp wrapper class array, just add your items directly into the list: 但由于你已经在使用
for
循环,我不介意使用临时包装类数组,只需将你的项目直接添加到列表中:
int[] intArray = new int[] { 0, 1 };
intList = new ArrayList<Integer>();
for(int intValue : intArray) {
intList.add(intValue);
}
With Java 8 you can do it in one line: 使用Java 8,您可以在一行中完成:
int[] intArr = { 1, 1, 2, 3, 5, 8, 11}; List<Integer> list = Arrays.stream(intArr).boxed().collect(Collectors.toList()); list.forEach(System.out::println);
It doesn't work because you're using an int[]
instead of Integer[]
. 它不起作用,因为你使用
int[]
而不是Integer[]
。 Try this: 试试这个:
Integer[] intArray = new Integer[] { 0, 1 };
// this should work now
intList = new ArrayList<Integer>(Arrays.asList(intArray));
The issue is about Autoboxing. 问题是关于Autoboxing。 Java automatically converts
int
to Integer
automatically, but it does not convert int[]
to Integer[]
. Java会自动将
int
自动转换为Integer
,但不会将int[]
转换为Integer[]
。 Hence the reason. 因此原因。
public static <T> List<T> asList(T... a)
asList
is defined as above. asList
定义如上。 It expects a vararg of type T
. 它期望
T
型的vararg。 ie it can take an array of objects of type T
. 即它可以采用
T
类型的对象数组。 In your case because Java cannot convert int[]
to Integer[]
, hence it takes type T
as int[]
rather than Integer
as desired. 在您的情况下,因为Java无法将
int[]
转换为Integer[]
,因此它将类型T
视为int[]
而不是Integer
。 Hence you get a list of type List<int[]
. 因此,您将获得
List<int[]
类型的List<int[]
。 You will have to manually convert it into `Integer[]
from int[]
你必须从
int[]
手动将其转换为`Integer[]
int[]
There is an excellent item in the book Effective Java
by Joshua Bloch where he explains the pits with varargs and this is one of them. Joshua Bloch在“
Effective Java
”一书中有一个很好的项目,他用varargs解释了坑,这就是其中之一。
Collection wants Objects and primitives don't derive from Object. 集合想要对象和基元不是从Object派生的。 so
Integer
allow but int
is not allow. 所以
Integer
允许但int
不允许。
So you have to use wrapper class Integer
instead of int
which is primitive type. 所以你必须使用包装类
Integer
而不是int
这是原始类型。
Just example: 举个例子:
Integer[] intArray = new Integer[] { 0, 1 };
intList = new ArrayList<Integer>(Arrays.asList(intArray));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.