Introduction
An array of more than one dimension is known as multi-dimensional array. Two dimensional array is actually an array of one dimensional array. This is unlike languages like C, Java array to have rows of varying length i.e. a multidimensional array can have 2 columns in one row and 3 columns in second.
Similar to one dimensional array, length of two dimensional array is also fixed. You can not change length of any array, i.e. number of rows and columns will remain fixed. Array index start at 0 and finishes at length -1.
// 2D integer array with 4 rows and 2 columns int[][] multiples = new int[4][2]; // Must specify first dimension for 2D int[][] wrong = new int[][]; // not OK, you must specify 1st dimension int[][] right = new int[2][]; // Correct
Initializing array
There are two ways to initialize a two dimensional array, either by using array literal at the time of creation or by using nested for loop and going through each element.
// Initializing two dimensional array as literal String[][] names = { {"Sam", "Smith"}, {"Robert", "Delgro"}, {"James", "Gosling"} }; // Initializing two dimensional array using loop int[][] board = new int[3][3]; for (int i = 0; i < board.length; i++) { for (int j = 0; j < board[i].length; j++) { board[i][j] = i + j; } }
Java doesn’t support multi-dimensional array in true sense. In a true two dimensional array all the elements of array occupy a contiguous block of memory. In Java, a multi-dimensional array is an array of array. For example two dimensional array is an array of one dimensional array like String[][] is an array of array of String[] or “array of array of strings”. This diagram shows how exactly two dimensional arrays are stored:
Because of above reason, second dimension in array is optional.
Random shuffling
To shuffle all the elements in a two dimensional array, for each element matrix[i][j], randomly generate indices i1 and j1 and swap matrix[i][j] with matrix[i1][j1], as follows:
// Random shuffling for (int i = 0; i < matrix. length; i++) { for (int j = 0; j < matrix[i].length; j++) { // Math.random() gererate number of type double in between (0, 1) int i1 = (int)(Math.random() * matrix.length); int j1 = (int)(Math.random() * matrix[i].length); // Swap matrix[i][j] with matrix[i1][j1] int temp = matrix[i][j]; matrix[i][j] = matrix[i1][j1]; matrix[i1][j1] = temp; } }
Passing 2D Arrays to Methods
Two dimensional array are passed to a method just as one dimensional array. Below example shows 2 methods. The first method, getArray(), returns a two dimensional array, and the second method, sum(int[][] m) which accepts 2D matrix.
// Program to illustrate the pass of 2D array as argument to function // and return 2D array from function public class PassTwoDimensionalArray { // Returns 2D array public static int[][] getArray() { // Make a Scanner, read user input Scanner input = new Scanner(System.in); // Enter array values int[][] m = new int[3][4]; for (int i = 0; i < m.length; i++) { for (int j = 0; j < m[i].length; j++) { m[i][j] = input.nextInt(); } } return m; } // Accept 2D array as function argument public static int sum(int[][] m) { int total = 0; for (int row = 0; row < m.length; row++) { for (int column = 0; column < m[row].length; column++) { total += m[row][column]; } } return total; } public static void main(String[] args) { // Get an 2D array int[][] m = getArray(); // Pass in 2D array and display sum of elements System.out.println("Sum of all elements is " + sum(m)); } }