如何将int 数组转换为List?
我希望这段代码显示为true
:
int[] array = {1, 2}; System.out.println(Arrays.asList(array).contains(1));
Arrays.asList(array)
将生成int[]
的单例列表。
如果将int[]
更改为Integer[]
它可以正常工作。 不知道这对你有帮助吗。
方法Arrays.asList(T ...)
是,当generics被擦除并且变换varargs时,实际上等于Arrays.ofList(Object[])
类型的方法(这是二进制等价,JDK 1.4版本的同样的方法)。
一个基元数组是一个Object
(也参见这个问题 ),但不是Object[]
,因此编译器认为您正在使用varargs版本并在int数组周围生成一个Object数组。 您可以通过添加额外步骤来说明正在发生的事情:
int[] array = {1, 2}; List listOfArrays = Arrays.asList(array); System.out.println(listOfArrays.contains(1));
这编译并等同于您的代码。 它显然也会返回错误。
编译器将varargs调用转换为具有单个数组的调用,因此调用需要参数T ...
的参数T t1, T t2, T t3
的varargs方法相当于用new T[]{t1, t2, t3}
调用它但这里的特殊情况是,如果方法需要一个对象数组,那么在创建数组之前,带有基元的varargs将被自动装箱。 因此编译器认为int数组作为单个Object传入,并创建一个Object[]
类型的单个元素数组,并将其传递给asList()
。
所以这里是上面的代码,编译器在内部实现它的方式:
int[] array = {1, 2}; // no generics because of type erasure List listOfArrays = Arrays.asList(new Object[]{array}); System.out.println(listOfArrays.contains(1));
以下是使用int值调用Arrays.asList()
一些好方法和坏方法:
// 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 good1 = Arrays.asList(1,2,3); // here we create an Integer[] array, and fill it with boxed ints List 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 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 ugly2 = Arrays.asList( Integer.valueOf(1), Integer.valueOf(2), Integer.valueOf(3) ); // And these versions produce compile errors: // compile error, type is List List bad1 = Arrays.asList(new int[]{1,2,3}); // compile error, type is List
参考:
- Java教程 > 类和对象 >将信息传递给方法或构造函数 > Varargs
-
Arrays.asList(T ...)
但要以一种简单的方式实际解决您的问题:
Apache Commons / Lang(参见Bozho的答案 )和Google Guava中有一些库解决方案:
-
Ints.contains(int[], int)
检查Ints.contains(int[], int)
数组是否包含给定的int -
Ints.asList(int ...)
从int数组创建一个Integers List
Arrays.asList(ArrayUtils.toObjectArray(array))
( ArrayUtils
来自commons-lang )
但是如果你只想调用contains
,则不需要。 只需使用Arrays.binarySearch(..)
(首先对数组进行排序)
这个
System.out.println(Arrays.asList(array).contains(array));
返回true
。
看来你对Arrays.asList(T... a)
理解是错误的。 你不会是第一个对其运作方式做出假设的人。
尝试一下
System.out.println(Arrays.asList(1, 2).contains(1));
在这种情况下,自动装箱并不像您希望的那样工作。 以下代码可能有点冗长,但是将int数组转换为列表的工作:
List list = new ArrayList (array.length); for (int value : array) { list.add(value); }
以下代码显示为true:
Integer[] array = {1, 2}; System.out.println(Arrays.asList(array).contains(1));
(你的版本失败了,因为Int不是对象,但是Int []是一个对象。因此你将调用asList(T … a),其中一个元素是一个Collection,因为它不可能有一个Collection a。)
你打电话时
Arrays.asList(array)
在你的基元数组上,你得到一个包含一个对象的List实例:一个int值数组! 你必须先将基元数组转换成一个对象数组,正如@Bozho在他的回答中所建议的那样。
如果您只想检查数组是否包含某些元素,只是迭代数组并搜索元素。 这将需要o(n / 2)。 所有其他解决方案效果较差。 将数组复制到列表的任何方法都必须遍历数组,因此此操作仅需要n个primefaces赋值。
我不认为你可以使用方法调用。 试试吧
List list = new ArrayList (); for (int index = 0; index < array.length; index++) { list.add(array[index]); }