Pavlo Maistrenko
Pavlo Maistrenko

Reputation: 347

Why is declaring an empty array of non-empty array(s) legal in Java?

Curious why declaring an empty array of non-emtpy array(s) is legal in Java:

int[][] array = new int[0][1];

System.out.println(array[][0]); //won't compile.
System.out.println(array[0][0]) //triggers an out of bounds exception.

P.S. I have read the related question on zero-size arrays: Why does Java allow arrays of size 0?

Is this for the same reason?

Upvotes: 6

Views: 749

Answers (4)

PeMa
PeMa

Reputation: 1716

It is legal to define empty arrays in general, no matter what it contains. Thus, it is also legal to have an empty array containing non empty arrays.

Mathematically (group theory) speaking, empty arrays are not only legal, but necessary, since they represent the zero (or neutral) element of array concatenation operation. This also makes it useful for programming (see the example below).

In your example you basically probe, if it is ok to access elements of an empty array. This is of course not legal, since there are none. However you can do:

int[][] array = new int[0][1];
System.out.println(array.length);

With reference to my own example above, a more useful case is:

    int[][] array1 = new int[1][1];
    int[][] array2 = new int[0][1];

    array1[0][0] = 1;

    int [][] concat = Stream
            .concat(Arrays.stream(array1), Arrays.stream(array2))
            .toArray(int[][]::new);

    System.out.println(Arrays.deepToString(concat));

Thus empty arrays allow to for "good" code, without ifs to exclude, illegal cases, which actually are totally fine.

Upvotes: 1

Abra
Abra

Reputation: 20914

An array dimension of zero means the array has zero elements. Hence there is no element at index 0 (zero).

When you declare a two-dimensional array in Java, the first array dimension is mandatory.

Refer to chapter 10 of the Java Language Specification.

Upvotes: 0

Eran
Eran

Reputation: 393936

An int[][] array is an array whose elements are int[] (i.e. its elements are arrays of int).

Just like you are allowed to define an empty array of int elements:

int[] empty = new int[0];

or an empty array of String elements:

String[] empty = new String[0];

You are also allowed to define an empty array of int[1] elements:

int[][] empty = new int[0][1];

Perhaps it's the syntax that is somewhat confusing here.

If it was

int[][] empty = new (int[1])[0]

it would be clearer that you are defining an empty array whose element type is int[1].

However, since the number in the first square brackets represents the number of elements in the outer array, new int[1][0] does not represent an empty array, but an array of a single element (whose single element is an empty int array).

Upvotes: 2

Mahamudul Hasan
Mahamudul Hasan

Reputation: 2823

as per your declaration:

int[][] array = new int[0][1];

you have declared 2D array where length of the array is 0 it means it will not be able to contain any element thats why

System.out.println(array[0][0]). array[0][0] is out of bound index error

Upvotes: 0

Related Questions