java ArrayList<Boolean> 类型中的 toArray(T[]) 方法不适用于参数 (boolean[])

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15715578/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 20:36:58  来源:igfitidea点击:

The method toArray(T[]) in the type ArrayList<Boolean> is not applicable for the arguments (boolean[])

javaarraysarraylist

提问by chanjianyi

I write a function to get booleanarray, from arraylist<boolean>to booleanarray 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[]opto Boolean[]op, that is work, but I need boolean[]..

如果我将类型更改boolean[]opBoolean[]op,那就是工作,但我需要boolean[]..

So how can i get the booleanarray?

那么我怎样才能得到boolean数组呢?

回答by ApproachingDarknessFish

Primitive types cannot be used in generics. In order for the array to match the type signature of T[], you must use the Booleanwrapper 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[],您必须Boolean像在 ArrayList 的声明中一样使用包装类,即使您不希望最终数组是该类型:

Boolean[] op = fool.toArray(new Boolean[fool.size()]);

There is no way to get an array of primitive booleans using generics, and autoboxing or casting does not work with arrays (e.g. it is impossible to assign arrays of primitive types to arrays of the wrapper types and vice-versa). The only way to get the array you want is to do it the hard way, with loops:

没有办法boolean使用泛型获得原始数组,并且自动装箱或强制转换不适用于数组(例如,不可能将原始类型数组分配给包装类型数组,反之亦然)。获得您想要的数组的唯一方法是使用循环以艰难的方式进行:

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 mapconstruct in Java 8.

这可能可以使用mapJava 8 中的构造更优雅地完成。

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. 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; having to write this kind of code is enough to make me want to switch to C++.

就个人而言,泛型不适用于基元这一事实是 Java 中为数不多的真正让我感到沮丧的事情之一。我不会与设计者争论,但是必须编写七个不同的函数来对不同类型的数组做同样的事情,这似乎无视首先向语言添加泛型特性的全部意义。看看java.util.Arrays 类;不得不写这种代码就足以让我想切换到 C++。

回答by Bhesh Gurung

If you really want boolean[]then populating list and finally converting list into an array shouldn't be necessary. Just do the following:

如果您真的想要,boolean[]则不需要填充列表并最终将列表转换为数组。只需执行以下操作:

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. It works only for reference types (i.e. objects).

注意:fool.toArray(new boolean[fool.size()])不起作用的事实是因为该方法是泛型方法,并且在 Java 中泛型不适用于原语。它仅适用于引用类型(即对象)。