java中的混合类型和混合数组类型数组Object[]未编译
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22553549/
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
Mixed type and mixed array type array Object[] in java not compiling
提问by user3435580
Here is how it looks like
这是它的样子
public Object[] settings = {true, true, false, 1, true, false, 10, 10, 20, false, false, false, false, false, {true, true, true, true}};
Error:
错误:
illegal initializer for java.lang.Object
In another IDE I get this error.
在另一个 IDE 中,我收到此错误。
Static Error: Array initializer must be assigned to an array type
采纳答案by vikingmaster
Initialize Array like this:
像这样初始化数组:
public Object[] settings = new Object[]{true, true, false, 1};
However, you cannot have arrays and values in the same dimension, because every element in a dimension must of the same type. (Strictly array '{}'
OR Object
in our case)
但是,您不能在同一维度中拥有数组和值,因为维度中的每个元素都必须是相同的类型。(在我们的情况下严格array '{}'
或Object
)
new Object[]{true, true, false, 1, {true, false} }; //<--- Illegal initializer
Instead just use several dimensions and group values in arrays:
相反,只需在数组中使用多个维度和组值:
public Object[][] settings = new Object[][]{{true, true}, {false, 1, 3}};
Either use ArrayList
or LinkedList
where it is possible to create any array you like.
使用ArrayList
或LinkedList
可以创建您喜欢的任何数组。
Update
更新
In fact it is possible to mix elements like this:
事实上,可以像这样混合元素:
new Object[]{true, false, 1, new Object[]{true, false} };