Error: illegal initializer for Object

0

I hope you can help me.

I have something of this style in a code in a .java file:

objeto = new Object[] { 
   { H1, H11, H12, H13}, 
   { H2, H21, H22, H23} 
}

But the error marks me:

illegal initializer for Object

Am I wrongly initializing my object so it marks me the error? What forms do I have to initialize it? I must have that structure, thanks for the help.

    
asked by EriK 04.01.2018 в 18:59
source

3 answers

2

Greetings, Erik.

The error that appears to you, the truth is for something quite simple, and is that you missed adding a pair of square brackets.

This is due to the fact that when initializing any type of Array, the number of parentheses indicates the dimensions it will have.

This means that you are declaring objeto as a matrix of Object of a dimension (that is, Object[] ), but, moment to fill that matrix that you created (which you do with the {H1, H11....} keys) you are indicating that you fill two dimensions (you realize when adding a comma, each , indicates a column) when actually the matrix is only of one.

In summary, your problem is solved by doing this:

objeto = new Object[][] { 
   { H1, H11, H12, H13}, 
   { H2, H21, H22, H23} 
}
    
answered by 05.01.2018 в 03:56
0

What you are trying to do, you can do in these two ways:

String[][] arrays = { array1, array2, array3, array4, array5 };

Or else

String[][] arrays = new String[][] { array1, array2, array3, array4, array5 };
  

The last syntax can be used in assignments other than the   of the declaration of the variable, while the shorter syntax   it only works with the declarations.

    
answered by 04.01.2018 в 19:15
0

Good EriK, the initialization is wrong. The structure is not adequate. First you must declare that your object variable is going to be an Object type array and then the structure should be in the following way:

Object[] objeto = new Object[] { H1, H11, H12, H13, H2, H21, H22, H23};

I hope it serves you. Greetings.

    
answered by 04.01.2018 в 19:17