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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 16:26:50  来源:igfitidea点击:

Mixed type and mixed array type array Object[] in java not compiling

javaarraysobjectcompiler-errorsmixed

提问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 Objectin 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 ArrayListor LinkedListwhere it is possible to create any array you like.

使用ArrayListLinkedList可以创建您喜欢的任何数组。



Update

更新

In fact it is possible to mix elements like this:

事实上,可以像这样混合元素:

new Object[]{true, false, 1, new Object[]{true, false} };