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.

Christophe Roussy
  • 16,299
  • 4
  • 85
  • 85
hermit
  • 1,048
  • 1
  • 6
  • 16
  • @TimBiegeleisen After assignment the row length changes to 5. – hermit Apr 11 '18 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. – Tim Biegeleisen Apr 11 '18 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 – XtremeBaumer Apr 11 '18 at 11:12
  • @XtremeBaumer so, it seems the second dimension does not matter.. – hermit Apr 11 '18 at 11:13
  • What are you using this for ? – Christophe Roussy Apr 11 '18 at 11:26
  • For performance consider other approaches: https://stackoverflow.com/questions/529457/performance-of-java-matrix-math-libraries – Christophe Roussy Apr 11 '18 at 11:35

3 Answers3

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.

Med Elgarnaoui
  • 1,612
  • 1
  • 18
  • 35
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.

Christophe Roussy
  • 16,299
  • 4
  • 85
  • 85
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.

Masáč
  • 163
  • 1
  • 5