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

1

13

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.

Static Error: Array initializer must be assigned to an array type
Paramaribo answered 21/3, 2014 at 8:19 Comment(1)
{true, true, true, true} is not valid Java syntax, except for in new something[] {true, true, true, true} or something[] something = {true, true, true, true}Meistersinger
R
13

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)

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.


Update

In fact it is possible to mix elements like this:

new Object[]{true, false, 1, new Object[]{true, false} };
Radiothermy answered 21/3, 2014 at 8:24 Comment(1)
public Object[] settings = {true, true, false, 1, true, false, 10, 10, 20, false, false, false, false, false, new Object[] {true, true, true, true}}; worked for me thanksParamaribo

© 2022 - 2024 — McMap. All rights reserved.