Java arraylist 找不到构造函数,使用 arrays.aslist
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10858476/
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
Java arraylist cannot find constructor, using arrays.aslist
提问by user16647
I'm using the Arrays.asList().contains() method in my code, as shown in the top answer: How can I test if an array contains a certain value?, so I am going to use Arrays.asList() in the code.
我在我的代码中使用 Arrays.asList().contains() 方法,如顶部答案所示:如何测试数组是否包含某个值?,所以我将在代码中使用 Arrays.asList()。
However, the compiler rejects this following code. Is it because of using primitives for my primes array, rather than a reference type? I don't think so, due to autoboxing, but I just wanted to check.
但是,编译器拒绝以下代码。是不是因为我的素数数组使用了原语,而不是引用类型?由于自动装箱,我不这么认为,但我只是想检查一下。
import java.math.*;
import java.util.ArrayList;
import java.util.Arrays;
public class .... {
public static void main(String[] args) {
int[] primes = formPrimes(15);
ArrayList<Integer> primes1 = new ArrayList<Integer>(Arrays.asList(primes));
// Rest of code...
}
public static int[] formPrimes(int n) {
// Code that returns an array of integers
}
}
I get one error, a cannot find symbol error.
我收到一个错误,一个找不到符号错误。
symbol : constructor ArrayList(java.util.List)
符号:构造函数 ArrayList(java.util.List)
location: class java.util.ArrayList ArrayList primes1 = new ArrayList(Arrays.asList(primes));
位置:class java.util.ArrayList ArrayList primes1 = new ArrayList(Arrays.asList(primes));
Basically, I've got a function returning an array of integers, and I want to convert it into an array list, and I'm running into trouble with using the ArrayList constructor.
基本上,我有一个返回整数数组的函数,我想将其转换为数组列表,但在使用 ArrayList 构造函数时遇到了麻烦。
回答by Jeshurun
Yes. Autoboxing does not apply to arrays, only to primitives.
是的。自动装箱不适用于数组,仅适用于原语。
The error I get in eclipse is
The constructor ArrayList<Integer>(List<int[]>) is undefined
我在 eclipse 中得到的错误是
The constructor ArrayList<Integer>(List<int[]>) is undefined
Thats because the constructor in ArrayList is defined as public ArrayList(Collection<? extends E> c)
. As you can see, it only accepts a subtype of Collection, which int is not.
那是因为 ArrayList 中的构造函数被定义为public ArrayList(Collection<? extends E> c)
. 如您所见,它只接受 Collection 的一个子类型,而 int 不接受。
Just change your code to:
只需将您的代码更改为:
public class .... {
public static void main(String[] args) {
Integer[] primes = formPrimes(15);
ArrayList<Integer> primes1 = new ArrayList<Integer>(Arrays.asList(primes));
// Rest of code...
}
public static Integer[] formPrimes(int n) {
// Code that returns an array of integers
}
}
and all should be well, assuming you return an Integer array from fromPrimes
.
一切都应该没问题,假设您从fromPrimes
.
UpdateFrom Andrew's comments, and after peeking into the source of Arrays.asList:
更新来自 Andrew 的评论,并在查看 Arrays.asList 的源代码后:
public static <T> List<T> asList(T... a) {
return new ArrayList<T>(a);
}
So what is really happening here is that Arrays.asList(new int[] {})
would actually return a List<int[]>
, unlike Arrays.asList(new Integer[] {})
which would return aList<Integer>
. Obviously the ArrayList constructor will not accept a List<int[]>
, and hence the compiler complains.
那么,什么是真正发生在这里的是,Arrays.asList(new int[] {})
实际上会返回一个List<int[]>
,不像Arrays.asList(new Integer[] {})
这将返回List<Integer>
。显然 ArrayList 构造函数不会接受 a List<int[]>
,因此编译器会抱怨。
回答by Carl Manaster
primes
is an array of the primitive int
type; that type does not derive from Object
and can therefore be automatically placed into a List
(which, like all Collections
, can only hold Object
s). @Justin is right; you need to manually add the items from your array to the list.
primes
是原始int
类型的数组;该类型不派生自Object
,因此可以自动放入 a List
(与 all 一样Collections
,只能容纳Object
s)。@贾斯汀是对的;您需要手动将数组中的项目添加到列表中。
回答by Ricky Clarkson
If you're doing this to get access to the contains method you may as well write your own.
如果您这样做是为了访问 contains 方法,您也可以编写自己的方法。
public boolean contains(int[] array, int item) {
for (int element: array)
if (element == item)
return true;
return false;
}