1

In the code below,I have defined a (2x2) 2D array, arr. And then I declare a one dimensional String array a with 5 elements, and assign it to the first row of the 2D array. Why am I allowed to do this, and why does not it throw array index out of bound exception?

I think it may be due to the reference assignment but still I am confuse about how one is allowed to change the array dimension at runtime.

String a[] = { "1", "2", "3", "4", "5" };

String[][] arr= new String[2][2];

int x;

arr[0] = a;
x = arr[0].length;
System.out.print("The row length is "+ x);

Output:The row length is 5.

6
  • @TimBiegeleisen After assignment the row length changes to 5. Commented Apr 11, 2018 at 11:07
  • Wow, I didn't know this, sort of. arr[0] is a reference to a 1D array, and you may make any assignment you wish. In Java, 2D (or 3D, etc.) array can be jagged, meaning that the second dimensions do not all have to be the same. Commented Apr 11, 2018 at 11:09
  • 1
    For reference, you don't have to define the 2nd dimension's length at all. String[][] arr = new String[1][]; is already valid, whereas it throws an error if you either remove the the size for first dimension or just initialize the size of the second dimension Commented Apr 11, 2018 at 11:12
  • @XtremeBaumer so, it seems the second dimension does not matter.. Commented Apr 11, 2018 at 11:13
  • What are you using this for ? Commented Apr 11, 2018 at 11:26

3 Answers 3

1

Java consider that arr[0] = a; as an array that's initialize with a .

this An asymmetric multidimensional array. look at this picture.

enter image description here

Another way to create an asymmetric array is to initialize just an array’s fi rst dimension, and defi ne the size of each array component in a separate statement:

int [][] args = new int[4][];
args[0] = new int[5];
args[1] = new int[3];

This technique reveals what you really get with Java: arrays of arrays that, properly managed, offer a multidimensional effect.

Sign up to request clarification or add additional context in comments.

Comments

1

What actually happens is nested arrays (same as in C/C++):

array[][] (actually just array[])
+---+ +---+ +---+ +---+ +---+
|   | |   | |   | |   | |   | .
| 0 | | 1 | | 2 | | 3 | | 4 | .
|   | |   | |   | |   | |   | .
+-+-+ +-+-+ +---+ +---+ +---+
  |     |
  v     v
arr[] arr[]
+---+ +---+
|   | |   |
| 0 | | 0 |
|   | |   |
+---+ +---+
|   | |   |
| 1 | | 1 |
|   | |   |
+---+ +---+
|   | |   |
| 2 | | 2 |
|   | |   |
+---+ +---+
 ...   ...

So first you access array[], and then another array (the nested one). If you want raw performance, avoid doing this, use a special math matrix lib. Also if you have a lot of empty cells in the matrix, consider using Maps instead. And for the fancy ascii stuff I use this.

Comments

0

There are no multi-dimensional arrays in Java.

String[][] describes one dimensional array of one dimensional arrays. It is not necessary that each sub array must have the same length.

new String[2][2] is just syntactic sugar for

String[][] arr = new String[2][]
arr[0] = new String[2];
arr[1] = new String[2];

arr[0] = a reassigns the reference from two element String array to another 5-elemen one. The 2 element array is no longer referenced and occasionally GC'ed.

To copy elements from a to arr use:

System.arraycopy(a, 0, arr[0], 0, a.length);

Which will throw the exception.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.