A multi-dimensional array in Java is an array of arrays. It allows you to store data in more than one dimension, such as rows and columns, or even higher dimensions like three-dimensional grids. Multi-dimensional arrays are often used to represent tables, matrices, and grids.
Characteristics of Multi-Dimensional Arrays:
-
Array of Arrays: Each element of a multi-dimensional array is itself an array.
-
Fixed Size: Like single-dimensional arrays, multi-dimensional arrays have a fixed size once declared.
-
Accessing Elements: You access the elements using multiple indices, one for each dimension.
Syntax for Declaring and Initializing Multi-Dimensional Arrays:
-
Declaration: A multi-dimensional array can be declared using the following syntax:
type[][] arrayName;
Code language: CSS (css)
type
: The data type of the elements (e.g.,int
,String
,double
).arrayName
: The name of the array variable.
2. Initialization: You can initialize a multi-dimensional array in various ways, including using curly braces {}
.
Example 1 (Declaration and Initialization at the Same Time):
int[][] matrix = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
Example 2 (Declaration First, then Initialization):
int[][] matrix = new int[3][3]; // Creates a 3x3 array (3 rows and 3 columns)
matrix[0][0] = 1;
matrix[0][1] = 2;
matrix[0][2] = 3;
matrix[1][0] = 4;
matrix[1][1] = 5;
matrix[1][2] = 6;
matrix[2][0] = 7;
matrix[2][1] = 8;
matrix[2][2] = 9;
Code language: JavaScript (javascript)
Accessing Elements in a Multi-Dimensional Array:
You can access the elements in a multi-dimensional array by providing multiple indices, one for each dimension.
Example:
int value = matrix[1][2]; // Accessing the element in the second row, third column
System.out.println(value); // Output: 6
Code language: JavaScript (javascript)
Iterating Through a Multi-Dimensional Array:
You can loop through multi-dimensional arrays using nested loops. The outer loop iterates over the rows, and the inner loop iterates over the columns.
Example:
for (int i = 0; i < matrix.length; i++) { // Outer loop for rows
for (int j = 0; j < matrix[i].length; j++) { // Inner loop for columns
System.out.print(matrix[i][j] + " ");
}
System.out.println();
}
Code language: PHP (php)
Example Program Using a Multi-Dimensional Array:
Here’s an example that declares a 3×3 matrix, fills it with values, and prints them:
public class MultiDimensionalArrayExample { public static void main(String[] args) { // Declare and initialize a 2D array (3x3 matrix) int[][] matrix = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} }; // Accessing and printing specific elements System.out.println("Element at [0][0]: " + matrix[0][0]); System.out.println("Element at [2][2]: " + matrix[2][2]); // Iterating through the 2D array using nested loops System.out.println("\nMatrix elements:"); for (int i = 0; i < matrix.length; i++) { for (int j = 0; j < matrix[i].length; j++) { System.out.print(matrix[i][j] + " "); } System.out.println(); // Move to the next row } } } /* Element at [0][0]: 1 Element at [2][2]: 9 Matrix elements: 1 2 3 4 5 6 7 8 9 */
Jagged Arrays (Arrays with Varying Row Lengths):
In Java, multi-dimensional arrays do not have to have equal-sized rows. This is called a jagged array. You can create a jagged array where each row has a different length.
int[][] jaggedArray = new int[3][];
jaggedArray[0] = new int[2]; // First row with 2 elements
jaggedArray[1] = new int[3]; // Second row with 3 elements
jaggedArray[2] = new int[1]; // Third row with 1 element
// Initializing the jagged array
jaggedArray[0][0] = 1;
jaggedArray[0][1] = 2;
jaggedArray[1][0] = 3;
jaggedArray[1][1] = 4;
jaggedArray[1][2] = 5;
jaggedArray[2][0] = 6;
// Printing the jagged array
System.out.println("Jagged Array:");
for (int i = 0; i < jaggedArray.length; i++) {
for (int j = 0; j < jaggedArray[i].length; j++) {
System.out.print(jaggedArray[i][j] + " ");
}
System.out.println(); // Move to the next row
}
Code language: PHP (php)
Jagged Array:
1 2
3 4 5
6
Code language: JavaScript (javascript)
Program: Jagged Array Example
public class JaggedArrayExample { public static void main(String[] args) { // Declare a jagged array with 3 rows (but the number of columns will vary per row) int[][] jaggedArray = new int[3][]; // Initialize each row with a different number of columns jaggedArray[0] = new int[2]; // First row with 2 elements jaggedArray[1] = new int[3]; // Second row with 3 elements jaggedArray[2] = new int[1]; // Third row with 1 element // Populate the jagged array with values jaggedArray[0][0] = 1; jaggedArray[0][1] = 2; jaggedArray[1][0] = 3; jaggedArray[1][1] = 4; jaggedArray[1][2] = 5; jaggedArray[2][0] = 6; // Accessing and printing individual elements System.out.println("Accessing individual elements:"); System.out.println("Element at [0][0]: " + jaggedArray[0][0]); // Output: 1 System.out.println("Element at [1][2]: " + jaggedArray[1][2]); // Output: 5 System.out.println("Element at [2][0]: " + jaggedArray[2][0]); // Output: 6 // Iterating over the jagged array using nested loops System.out.println("\nIterating over the jagged array:"); for (int i = 0; i < jaggedArray.length; i++) { for (int j = 0; j < jaggedArray[i].length; j++) { System.out.print(jaggedArray[i][j] + " "); } System.out.println(); // Move to the next row } // Printing the jagged array structure (toString representation) System.out.println("\nJagged array structure:"); for (int i = 0; i < jaggedArray.length; i++) { System.out.print("Row " + i + ": "); for (int j = 0; j < jaggedArray[i].length; j++) { System.out.print(jaggedArray[i][j] + " "); } System.out.println(); } } } /* Accessing individual elements: Element at [0][0]: 1 Element at [1][2]: 5 Element at [2][0]: 6 Iterating over the jagged array: 1 2 3 4 5 6 Jagged array structure: Row 0: 1 2 Row 1: 3 4 5 Row 2: 6 */
Multi-dimensional arrays in Java provide a way to store data in more complex structures like grids or tables. You can easily declare, initialize, and access elements in a multi-dimensional array using multiple indices. Additionally, with the use of nested loops, you can iterate over all elements in a multi-dimensional array. Jagged arrays allow for more flexible and dynamic array structures where the rows can have different lengths.