[英]The method toArray(T[]) in the type ArrayList<Boolean> is not applicable for the arguments (boolean[])
I write a function to get boolean
array, from arraylist<boolean>
to boolean
array but I get the error:我编写了一个函数来获取boolean
数组,从arraylist<boolean>
到boolean
数组,但出现错误:
The method toArray(T[]) in the type ArrayList is not applicable for the arguments (boolean[]) ArrayList 类型中的 toArray(T[]) 方法不适用于参数 (boolean[])
ArrayList<Boolean> fool = new ArrayList<Boolean>();
for (int i = 0; i < o.length(); i++) {
if (Integer.parseInt(o.substring(i, i + 1)) == 1) {
fool.add(true);
} else {
fool.add(false);
}
}
boolean[] op = fool.toArray(new boolean[fool.size()]);
if I change the type boolean[]op
to Boolean[]op
, that is work, but I need boolean[]
..如果我将类型boolean[]op
更改为Boolean[]op
,那是可行的,但我需要boolean[]
..
So how can i get the boolean
array?那么我怎样才能得到boolean
数组呢?
Primitive types cannot be used in generics.不能在泛型中使用原始类型。 In order for the array to match the type signature of T[]
, you must use the Boolean
wrapper class just as you did in the ArrayList's declaration, even if you don't want the final array to be of that type:为了使数组匹配T[]
的类型签名,您必须像在 ArrayList 的声明中一样使用Boolean
包装类,即使您不希望最终数组为该类型:
Boolean[] op = fool.toArray(new Boolean[fool.size()]);
There is no way to get an array of primitive boolean
s using generics, and autoboxing or casting does not work with arrays (eg it is impossible to assign arrays of primitive types to arrays of the wrapper types and vice-versa).无法使用泛型获取原始boolean
数组,并且自动装箱或强制转换不适用于数组(例如,不可能将原始类型的数组分配给包装类型的数组,反之亦然)。 The only way to get the array you want is to do it the hard way, with loops:获得您想要的数组的唯一方法是使用循环以艰难的方式进行:
boolean[] op = new boolean[fool.size()];
for(int n = 0; n < temp.length; n++)
{
//autoboxing implicitly converts Boolean to boolean
op[n] = fool.get(n);
}
This can probably be accomplished much more elegantly using the map
construct in Java 8.这可能可以使用 Java 8 中的map
结构更优雅地完成。
On a personal note, the fact that generics don't work for primitives is one of the few things about java that honestly really frustrates me.就个人而言,泛型不适用于基元这一事实是 Java 中为数不多的真正让我感到沮丧的事情之一。 I'm not going to argue with the designers, but having to write seven different functions just to do the same thing to arrays of different types seems to defy the whole point of adding a generics feature to the language in the first place.我不会与设计者争论,但是必须编写七个不同的函数来对不同类型的数组做同样的事情,这似乎无视首先向语言添加泛型特性的全部意义。 Just look at the java.util.Arrays class ;看看java.util.Arrays 类; having to write this kind of code is enough to make me want to switch to C++.不得不写这种代码就足以让我想切换到 C++。
If you really want boolean[]
then populating list and finally converting list into an array shouldn't be necessary.如果您真的想要boolean[]
则不需要填充列表并最终将列表转换为数组。 Just do the following:只需执行以下操作:
boolean[] bools = new boolean[o.length()];
for (int i = 0; i < o.length(); i++) {
bools[i] = Integer.parseInt(o.substring(i,i+1)) == 1;
}
Note: The fact that fool.toArray(new boolean[fool.size()])
doesn't work is because that method is a generic method and in Java generics doesn't work for primitives.注意: fool.toArray(new boolean[fool.size()])
不起作用的事实是因为该方法是泛型方法,并且在 Java 中泛型不适用于基元。 It works only for reference types (ie objects).它仅适用于引用类型(即对象)。
int index = 0;
boolean[] array = new boolean[fool.size()];
for (Boolean value: fool) {
array[index++] = value;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.