Java Tutorial
Operators
Flow Control
String
Number and Date
Built-in Classes
Array
Class and Object
Inheritance and Polymorphism
Exception Handling
Collections, Generics and Enumerations
Reflection
Input/Output Stream
Annotation
In Java, a multidimensional array is an array of arrays, meaning that it can have multiple levels of arrays. The most common type of multidimensional array is the two-dimensional array (2D array), which can be thought of as a table with rows and columns. This tutorial will cover the basics of creating and using multidimensional arrays in Java.
To create a two-dimensional array, you need to specify the number of rows and columns within two pairs of square brackets ([][]
).
int[][] matrix = new int[3][4]; // Creates a 3x4 (3 rows, 4 columns) integer array
You can initialize a two-dimensional array by specifying the values for each row within curly braces ({}
).
int[][] matrix = { {1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12} };
To access elements in a two-dimensional array, use two indices within square brackets, where the first index represents the row, and the second index represents the column.
int value = matrix[1][2]; // Retrieves the value at row 1, column 2 (7 in this example)
You can modify the elements in a two-dimensional array using assignment statements and indices.
matrix[0][0] = 42; // Changes the value at row 0, column 0 to 42
You can use nested loops to iterate over a two-dimensional array. The outer loop iterates over the rows, while the inner loop iterates over the columns.
for (int row = 0; row < matrix.length; row++) { for (int col = 0; col < matrix[row].length; col++) { System.out.print(matrix[row][col] + " "); } System.out.println(); }
A jagged array is a type of multidimensional array in which the length of each row can be different. To create a jagged array, you only need to specify the number of rows when you create the array. Then, you can create each row with a different length.
int[][] jaggedArray = new int[3][]; jaggedArray[0] = new int[4]; jaggedArray[1] = new int[2]; jaggedArray[2] = new int[3];
You can initialize a jagged array by specifying the values for each row within curly braces ({}
). Each row can have a different number of values.
int[][] jaggedArray = { {1, 2, 3, 4}, {5, 6}, {7, 8, 9} };
This tutorial introduced the basics of creating and using multidimensional arrays in Java, focusing on two-dimensional arrays and jagged arrays. You can use the same concepts to create arrays with more dimensions, although they are less common in practice.
Accessing elements in a 2D array in Java:
int[][] twoDArray = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; int element = twoDArray[1][2]; // Accesses the element at row 1, column 2 (value 6)
Working with 3D arrays in Java:
int[][][] threeDArray = {{{1, 2}, {3, 4}}, {{5, 6}, {7, 8}}}; int element = threeDArray[1][0][1]; // Accesses the element at layer 1, row 0, column 1 (value 6)
Iterating through multidimensional arrays in Java:
for (int i = 0; i < twoDArray.length; i++) { for (int j = 0; j < twoDArray[i].length; j++) { System.out.print(twoDArray[i][j] + " "); } System.out.println(); // Move to the next row }
Common operations and manipulations on multidimensional arrays:
int numRows = twoDArray.length; int numCols = twoDArray[0].length; int[][] copyArray = Arrays.copyOf(twoDArray, twoDArray.length);
Converting between 1D and 2D arrays in Java:
int[] oneDArray = twoDArray[1]; int[][] newTwoDArray = {oneDArray, {10, 11, 12}};
Initializing and populating a jagged array in Java:
int[][] jaggedArray = new int[3][]; jaggedArray[0] = new int[]{1, 2, 3}; jaggedArray[1] = new int[]{4, 5}; jaggedArray[2] = new int[]{6, 7, 8, 9};