简体   繁体   English

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

[英]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.

相关问题 方法put(String,ArrayList <Integer> ),类型为TreeMap <String,ArrayList<Integer> &gt;不适用于参数(字符串,布尔值) - The method put(String, ArrayList<Integer>) in the type TreeMap<String,ArrayList<Integer>> is not applicable for the arguments (String, boolean) 如何修复[ContentValues类型的put(String,Boolean)方法不适用于自变量(Boolean,Boolean)] - How to fix [The method put(String, Boolean) in the type ContentValues is not applicable for the arguments (Boolean, Boolean)] Integer 类型中的方法 parseInt(String) 不适用于参数(布尔值) - The method parseInt(String) in the type Integer is not applicable for the arguments (boolean) JspWriter类型的方法print(boolean)不适用于参数(void) - The method print(boolean) in the type JspWriter is not applicable for the arguments (void) PrintStream 类型中的方法 println(boolean) 不适用于参数 (void) - The method println(boolean) in the type PrintStream is not applicable for the arguments (void) Intent类型的方法putExtra(String,boolean)不适用于参数(String,CarouselDataItem) - The method putExtra(String, boolean) in the type Intent is not applicable for the arguments (String, CarouselDataItem) LayoutInflater类型的方法inflate(int,ViewGroup,boolean)不适用于参数(int,int,boolean) - The method inflate(int, ViewGroup, boolean) in the type LayoutInflater is not applicable for the arguments (int, int, boolean) 将textView转换为Boolean以避免:Intent类型中的方法putExtra(String,boolean)不适用于参数 - Converting textView to Boolean to avoid: The method putExtra(String, boolean) in the type Intent is not applicable for the arguments 方法直到(函数<!--? super WebDriver,V--> ) 在 FluentWait 类型中不适用于 arguments (boolean, ExpectedCondition<webelement> )</webelement> - The method until(Function<? super WebDriver,V>) in the type FluentWait is not applicable for the arguments (boolean, ExpectedCondition<WebElement>) 错误 java 方法组合Sum(int[], int, List<integer> ) 类型中的解决方案不适用于 arguments (int[], int, boolean)</integer> - error java The method combinationSum(int[], int, List<Integer>) in the type Solution is not applicable for the arguments (int[], int, boolean)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM