NoJaggedArrays
DRAFT

Misconception:

Multi-dimensional arrays have a rectangular (for two dimensions) or hyper-rectangular (for any number of dimensions) shape. For example, it’s not possible to have a multi-dimensional array a where a[0].length != a[1].length.

Incorrect

Multi-dimensional arrays have a rectangular shape

Correct

Multi-dimensional arrays can have a jagged shape

Correction
Here is what's right.

A mult-dimensional array in Java is just an array of arrays. Assume a two-dimensional array, where the outer array has n elements. Each of those n elements the outer array points to is a separate one-dimensional array. And as such, each of those n elements can have a separate length.

Note, though, that array creation expressions always create rectangular arrays:

String[][] cells = new String[100][80]; // rectangular

One can create a jagged array by using an array initializer:

int[][] triangle = new int[][] { {1, 2}, {3} }; // jagged

One also can create a jagged array by updating an array element of an outer array to point to an array with a different length:

double[][][] matrix = new double[4][4][4]; // hyper-rectangular
matrix[2][1] = new int[100]; // now matrix[2][1].length == 100
matrix[1] = new int[30][20]; // now matrix[1].length == 30

Stay up-to-date

Follow us on  twitter to hear about new misconceptions.