From the previous article Java Array Basics, we understand that an array is like a container that can hold a certain number of values. In this article, we will learn what is a two-dimensional array, how to initialize and access a two-dimensional array with examples.
In the Java programming language, a multidimensional array is an array whose components are themselves arrays.
Two Dimensional Array Simple Example
class MultiDimArrayDemo {
public static void main(String[] args){
String[][] names = {
{"Mr. ","Mrs. ","Ms. "},
{"Smith","Jones"}};
// Mr. Smith
System.out.println(names[0][0] +
names[1][0]);
// Ms. Jones
System.out.println(names[0][2] +
names[1][1]);
}
}
Output:
Mr. Smith
Ms. Jones
The following code prints the array's size to standard output:
System.out.println(anArray.length);
Let’s look at few examples of defining java two-dimensional array or 2d array.
Java Two Dimensional Array of primitive type
Let's create an array of int arrays of 2 row and 3 columns.
// an array of int arrays of 2 row and 3 columns
int[][] twoDimArray = new int[2][3];
for (int i = 0; i < twoDimArray.length; i++) {
for (int j = 0; j < twoDimArray[i].length; j++) {
twoDimArray[i][j] = j;
System.out.print(twoDimArray[i][j] + " ");
}
System.out.println("");
}
Output:
0 1 2
0 1 2
Java Two Dimensional Array of Objects
Let's create an array of String arrays of 3 rows and 4 columns.
// an array of String arrays of 3 rows and 4 columns
String[][] arrStr = new String[3][4];
for (int i = 0; i < arrStr.length; i++) {
for (int j = 0; j < arrStr[i].length; j++) {
arrStr[i][j] = "Str" + j;
System.out.print(arrStr[i][j] + " ");
}
System.out.println("");
}
Output:
Str0 Str1 Str2 Str3
Str0 Str1 Str2 Str3
Str0 Str1 Str2 Str3
Shortcut Syntax to Create and Initialize a Two Dimensional Array
Shortcut syntax to create and initialize an array:
// creating and initializing two dimensional int array with shortcut
// syntax
int[][] arrInt = { { 1, 2,3 }, { 3, 4, 5 } };
for (int i = 0; i < arrInt.length; i++) {
for (int j = 0; j < arrInt[i].length; j++) {
System.out.print(arrInt[i][j] + " ");
}
System.out.println("");
}
Complete Two Dimensional Array Source Code Example
/**
* This class will show how to initialize two dimensional array
* @author javaguides.net
*
*/
public class TwoDimentionalArray {
public static void main(String[] args) {
// an array of int arrays of 2 row and 3 columns
int[][] twoDimArray = new int[2][3];
for (int i = 0; i < twoDimArray.length; i++) {
for (int j = 0; j < twoDimArray[i].length; j++) {
twoDimArray[i][j] = j;
System.out.print(twoDimArray[i][j] + " ");
}
System.out.println("");
}
// an array of String arrays of 3 rows and 4 columns
String[][] arrStr = new String[3][4];
for (int i = 0; i < arrStr.length; i++) {
for (int j = 0; j < arrStr[i].length; j++) {
arrStr[i][j] = "Str" + j;
System.out.print(arrStr[i][j] + " ");
}
System.out.println("");
}
// creating and initializing two dimensional int array with shortcut
// syntax
int[][] arrInt = { { 1, 2,3 }, { 3, 4, 5 } };
for (int i = 0; i < arrInt.length; i++) {
for (int j = 0; j < arrInt[i].length; j++) {
System.out.print(arrInt[i][j] + " ");
}
System.out.println("");
}
}
}
References
Free Spring Boot Tutorial | Full In-depth Course | Learn Spring Boot in 10 Hours
Watch this course on YouTube at Spring Boot Tutorial | Fee 10 Hours Full Course