java 如何将 int[] 数组转换为 List?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4324633/
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
How to convert an int[] array to a List?
提问by tshepang
I expected this code to display true
:
我希望此代码显示true
:
int[] array = {1, 2};
System.out.println(Arrays.asList(array).contains(1));
采纳答案by aioobe
The Arrays.asList(array)
will result in a singleton list of an int[]
.
在Arrays.asList(array)
将导致的一个单列表int[]
。
It works as you expect if you change int[]
to Integer[]
. Don't know if that helps you though.
如果您更改int[]
为Integer[]
. 不知道这是否对你有帮助。
回答by Sean Patrick Floyd
The method Arrays.asList(T ...)
is, when generics are erased and varargs are transformed, actually equal to a method of type Arrays.ofList(Object[])
(which is the, binary equivalent, JDK 1.4 version of the same Method).
该方法Arrays.asList(T ...)
是,当泛型被擦除和可变参数被转换时,实际上等于一个类型的方法Arrays.ofList(Object[])
(它是相同方法的二进制等效的 JDK 1.4 版本)。
An array of primitives is an Object
(see also this question), but not an Object[]
, so the compiler thinks you are using the varargs version and generates an Object array around your int array. You could illustrate what's happening by adding an extra step:
原语数组是一个Object
(另请参见此问题),但不是Object[]
,因此编译器认为您使用的是可变参数版本并围绕您的 int 数组生成一个 Object 数组。您可以通过添加一个额外的步骤来说明正在发生的事情:
int[] array = {1, 2};
List<int[]> listOfArrays = Arrays.asList(array);
System.out.println(listOfArrays.contains(1));
This compiles and is equivalent to your code. It also obviously returns false.
这将编译并等效于您的代码。它也显然返回false。
The compiler translates varargs calls into calls with a single array, so calling a varargs method that expects parameters T ...
with parameters T t1, T t2, T t3
is equivalent to calling it with new T[]{t1, t2, t3}
but the special case here is that varargs with primitives will be autoboxed before the array is created if the method needs an object array. So the compiler thinks the int array is passed in as a single Object and creates a single element array of type Object[]
, which it passes to asList()
.
编译器把可变参数调用与单个阵列中调用,因此调用可变参数方法,它预计参数T ...
与参数T t1, T t2, T t3
等效于调用它new T[]{t1, t2, t3}
,但这里的特殊情况是可变参数与创建前阵元将被autoboxed,如果该方法需要一个对象数组。因此,编译器认为 int 数组作为单个 Object 传入,并创建一个类型为 的单个元素数组Object[]
,然后将其传递给asList()
。
So here's the above code once again, the way the compiler implements it internally:
所以这里又是上面的代码,编译器内部实现的方式:
int[] array = {1, 2};
// no generics because of type erasure
List listOfArrays = Arrays.asList(new Object[]{array});
System.out.println(listOfArrays.contains(1));
Here are some good and bad ways to call Arrays.asList()
with int values:
以下是一些Arrays.asList()
使用 int 值调用的好方法和坏方法:
// These versions use autoboxing (which is potentially evil),
// but they are simple and readable
// ints are boxed to Integers, then wrapped in an Object[]
List<Integer> good1 = Arrays.asList(1,2,3);
// here we create an Integer[] array, and fill it with boxed ints
List<Integer> good2 = Arrays.asList(new Integer[]{1,2,3});
// These versions don't use autoboxing,
// but they are very verbose and not at all readable:
// this is awful, don't use Integer constructors
List<Integer> ugly1 = Arrays.asList(
new Integer(1),new Integer(2),new Integer(3)
);
// this is slightly better (it uses the cached pool of Integers),
// but it's still much too verbose
List<Integer> ugly2 = Arrays.asList(
Integer.valueOf(1), Integer.valueOf(2), Integer.valueOf(3)
);
// And these versions produce compile errors:
// compile error, type is List<int[]>
List<Integer> bad1 = Arrays.asList(new int[]{1,2,3});
// compile error, type is List<Object>
List<Integer> bad2 = Arrays.asList(new Object[]{1,2,3});
Reference:
参考:
- Java Tutorial> Classes and Objects> Passing Information to a Method or a Constructor> Varargs
Arrays.asList(T ...)
But to actually solve your problem in a simple way:
但要以一种简单的方式实际解决您的问题:
There are some library solutions in Apache Commons / Lang (see Bozho's answer) and in Google Guava:
在 Apache Commons/Lang(参见Bozho 的回答)和Google Guava 中有一些库解决方案:
Ints.contains(int[], int)
checks whether an array of ints contains a given intInts.asList(int ...)
creates a List of Integers from an int array
Ints.contains(int[], int)
检查整数数组是否包含给定的整数Ints.asList(int ...)
从一个 int 数组创建一个整数列表
回答by Bozho
Arrays.asList(ArrayUtils.toObjectArray(array))
(ArrayUtils
is from commons-lang)
(ArrayUtils
来自commons-lang)
But if you want to just call contains
there is no need of that. Simply use Arrays.binarySearch(..)
(sort the array first)
但如果你只想打电话,contains
那就没有必要了。只需使用Arrays.binarySearch(..)
(先对数组进行排序)
回答by Boris Pavlovi?
This
这
System.out.println(Arrays.asList(array).contains(array));
returns true
.
返回true
。
回答by Qwerky
It seems like your understanding of Arrays.asList(T... a)
is wrong. You wouldn't be the first person to make an assumption as to how it works.
看来你的理解Arrays.asList(T... a)
是错误的。您不会是第一个对它的工作原理做出假设的人。
Try it with
试试看
System.out.println(Arrays.asList(1, 2).contains(1));
回答by dogbane
Autoboxing just doesn't work the way you want it to in this case. The following code may be a bit verbose, but does the job of converting an int array to a list:
在这种情况下,自动装箱无法按照您希望的方式工作。以下代码可能有点冗长,但可以将 int 数组转换为列表:
List<Integer> list = new ArrayList<Integer>(array.length);
for (int value : array) {
list.add(value);
}
回答by HaskellElephant
The following code displays true:
以下代码显示为真:
Integer[] array = {1, 2};
System.out.println(Arrays.asList(array).contains(1));
(Your version fails, since Int's not beeing objects, but Int[] is an object. Therefor you will call asList(T... a) with one element beeing a Collection, since it is not possible to have an Collection a.)
(您的版本失败,因为 Int 不是对象,但 Int[] 是一个对象。因此,您将调用 asList(T... a) ,其中一个元素是集合,因为不可能有集合 a。)
回答by AlexR
If you only want to check whether the array contains certain element just iterate over array and search for element. This will take o(n/2). All other solutions are less effective. Any method that copies array to list must iterate over array and therefore this operation only requires n atomic assignments.
如果您只想检查数组是否包含某个元素,只需遍历数组并搜索元素。这将需要 o(n/2)。所有其他解决方案都不太有效。任何将数组复制到列表的方法都必须遍历数组,因此该操作只需要 n 个原子赋值。
回答by Pointy
When you call
你打电话的时候
Arrays.asList(array)
on your array of primitives, you get a List instance containing one object: an array of int values! You have to first convert the array of primitives into an array of objects, as @Bozho suggests in his answer.
在原始数组上,您会得到一个包含一个对象的 List 实例:一个 int 值数组!您必须首先将原始数组转换为对象数组,正如@Bozho 在他的回答中所建议的那样。
回答by Jinesh Parekh
I dont think there is a method call you could use. Try it like this
我认为您没有可以使用的方法调用。像这样试试
List<Integer> list = new ArrayList<Integer>();
for (int index = 0; index < array.length; index++)
{
list.add(array[index]);
}