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 2D array is essentially an array of arrays. It can be thought of as a table with rows and columns, where each cell contains an element. Just like 1D arrays, 2D arrays are objects that can be created and manipulated dynamically.
In this tutorial, we'll cover the following topics related to 2D arrays in Java:
Declaration and Initialization
Looping through the 2D Array
Common 2D Array Operations
Array Dimensions
Declaration and Initialization:
There are several ways to declare and initialize a 2D array in Java:
A. Declare a 2D array and then initialize it:
int[][] matrix; // Declare a 2D int array matrix = new int[3][3]; // Initialize the array with 3 rows and 3 columns
B. Declare and initialize a 2D array in one line:
int[][] matrix = new int[3][3]; // Declare and initialize a 2D int array with 3 rows and 3 columns
C. Declare and initialize a 2D array with specific elements:
int[][] matrix = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} }; // Declare and initialize a 2D int array with specific elements
There are several ways to loop through a 2D array in Java:
A. Using nested for loops:
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(); }
B. Using nested enhanced for loops (also known as for-each loops):
for (int[] row : matrix) { for (int element : row) { System.out.print(element + " "); } System.out.println(); }
A. Adding elements to the array:
matrix[0][0] = 1; // Add element at row 0 and column 0 matrix[0][1] = 2; // Add element at row 0 and column 1
B. Updating elements in the array:
matrix[0][0] = 5; // Update element at row 0 and column 0
C. Accessing elements from the array:
int firstElement = matrix[0][0]; // Get the element at row 0 and column 0
To find the dimensions of a 2D array, use the length
attribute:
A. Number of rows:
int numberOfRows = matrix.length; // Get the number of rows in the array
B. Number of columns for a specific row:
int numberOfColumns = matrix[0].length; // Get the number of columns in the first row of the array
Note: Remember that the index of an array starts from 0 and goes up to the length of the array minus 1.
This tutorial should provide you with a basic understanding of 2D arrays in Java. With this knowledge, you can create, manipulate, and work with 2D arrays effectively in your Java programs.
Java 2D Array Definition:
// Define a 2D array of integers int[][] matrix;
Initializing 2D Arrays in Java:
// Initialize a 2D array of doubles double[][] scores = new double[3][4];
Assigning Values to a 2D Array in Java:
// Assign values to a 2D array scores[0][0] = 95.5; scores[1][2] = 88.0;
Java Array Instantiation for 2D Arrays:
// Instantiate a 2D array of characters char[][] ticTacToe = new char[3][3];
Java 2D Array Length and Size:
// Get the number of rows in a 2D array int numRows = ticTacToe.length;
Accessing Elements in a Java 2D Array:
// Access an element in a 2D array char centerSquare = ticTacToe[1][1];
Java 2D Array Initialization with Values:
// Initialize a 2D array with values int[][] matrix = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
Java Dynamic 2D Array Creation:
// Dynamic 2D array creation using ArrayList ArrayList<ArrayList<Integer>> dynamic2DArray = new ArrayList<>();
Java Jagged 2D Arrays:
// Jagged 2D array int[][] jaggedArray = {{1, 2, 3}, {4, 5}, {6, 7, 8, 9}};
Java Array Literals for 2D Arrays:
// Initialize a 2D array using literals char[][] gameBoard = {{'X', 'O', 'X'}, {'O', 'X', 'O'}, {'X', 'O', 'X'}};
Java Multidimensional Arrays vs. Nested Arrays:
// Multidimensional array int[][] multiArray = new int[3][4]; // Nested arrays int[][] nestedArray = new int[3][]; nestedArray[0] = new int[2]; nestedArray[1] = new int[3];
Java Array Memory Allocation for 2D Arrays:
// Memory allocation for a 2D array int[][] memoryArray = new int[3][4];
Java 2D Array Traversal with Loops:
// Traverse a 2D array using loops for (int i = 0; i < numRows; i++) { for (int j = 0; j < numCols; j++) { // Access element at (i, j) } }
Java Array Manipulation for 2D Arrays:
// Manipulate a 2D array (e.g., transpose) for (int i = 0; i < numRows; i++) { for (int j = i + 1; j < numCols; j++) { // Swap elements at (i, j) and (j, i) int temp = matrix[i][j]; matrix[i][j] = matrix[j][i]; matrix[j][i] = temp; } }
Java Array Sorting for 2D Arrays:
// Sort a 2D array (e.g., based on the first column) Arrays.sort(matrix, Comparator.comparingInt(row -> row[0]));
Java Copying 2D Arrays:
// Copy a 2D array int[][] copyArray = Arrays.stream(originalArray) .map(int[]::clone) .toArray(int[][]::new);