First statement: int[] it = new int[][]{{1}}[0]; //Valid Statement

Second statement: int[][]it2 = new int[]{0}[0][0]; //Invalid Statement

First one no explanation given.

Second one is invalid because it attempts to access a two-dimensional object from a single dimension array.

My query:
1. First of all can someone please interpret those two lines in simpler terms :(
2. What is the difference between the two statements that makes second invalid.
3. Finally why in first statement there are two curly braces, how is that interpreted, as in what it symbolizes?

Terribly confused. Please help. Thanks in advance.

P.S. This is from Devaka Cooray Exam Lab for SCJP Certification.

I know Java can be sometimes confusing when you're trying to learn something new about it. I think there is a problem with defining the array. However, Have a look the Java Tuturial website which explains one dimension and multi-dimension arrays.The link is here

Scroll down to the section mentioned below.

class MultiDimArrayDemo {
    public static void main(String[] args) {
        String[][] names = {
            {"Mr. ", "Mrs. ", "Ms. "},
            {"Smith", "Jones"}
        };
        // Mr. Smith
        System.out.println(names[0][0] + names[1][0]);
        // Ms. Jones
        System.out.println(names[0][2] + names[1][1]);
    }
}

Just if you post any piece of code next time, PLEASE ensure that you use the Code tags. Therefore, your post will be much easier to follow. Thanks.

The {} in this context define a literal array, as in

int a = 1;
int[] b = {1,2,3};
int[][] c = {{1,2,3},{4.5.6}}; // 2x3 "2D" array (array of arrays)

so {{1}} defines a 2D array 1 row, 1 column, containing the single value 1.
{{1}}[0] selects the first element of the first/outer array, ie the array {1}
So
int[][]{{1}}[0] defines a 2D array, selects the first element of the outer array, and thus returns the inner array {1}, which is a valid value for int[] it

(ps I wrote 2D array, but this isn't exactly correct, Java [][] arrays are arrays of arrays. The inner arrays can al be different lengths, so the [][] does not have to be rectangular)

Using James's explanation, you should be able to see why the second statement is invalid.

//Second statement:
int[][] it2 = new int[]{0}[0][0]; //Invalid Statement
//This means the initial value of 'new int[]' is {0}
//The [0] is to access the first element of {0} which is 0
//The 0 is a primitive integer, but the latter in the statement
//  attempt to access the 0 index of an array from the primitive.
//Therefore it is invalid.
commented: Taywin, I understood what James & rotten 69 mentioned, also in your explanation I understood {0} and first[0] part, I'm still unclear of the second [0] in the statement int[][] it2 = new int[]{0}[0][0]; +1

Taywin, I understood what James & rotten 69 mentioned, also in your explanation I understood {0} and first[0] part, I'm still unclear of the second [0] in the statement int[][] it2 = new int[]{0}[0][0];

rotten69 I'll make sure I use code tags from now on :) Thanks for the help :)

The second [0] is the error.
If there was a 2D array then that would be a valid index, eg

int[][] a = {{1,2,3},{4.5.6};
a[1][2] == 6

but because its a 1 D array ( new int[]{0} ) then the second index is invalid.

Awesome ! :) Thanks a ton James and Taywin & rotten69. It's clear now. I can close the the post finally as solved phew :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.