In this article, we will learn to initialize 2D array in Java.
The array is a data structure that is used to collect a similar type of data into contiguous memory space
. An array can be a single-dimensional or multidimensional. An array
that has 2 dimensions is called 2D or two-dimensional array
.
There are several ways to create and initialize a 2D array in Java. Let’s see some examples.
Table of Contents
Initialize 2D array Using for loop
This is the simplest approach in which we create an array and initialize every index using for loop.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
public class Main { public static void main(String args[]) { int count = 0; // Creating 2D array int a[][] = new int[3][2]; for(int i=0; i<3; i++) { for(int j=0; j<2; j++) { a[i][j] = count++; // Initialize Array System.out.print(a[i][j]+" "); } System.out.println(); } } } |
Output:
2 3
4 5
Initialize 2D array using an initializer
We can use an initializer block to initialize the array at the same time while creating an array. See the example below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
public class Main { public static void main(String args[]) { // Creating and Initializing 2D array int a[][] = {{0,1},{2,3},{4,5}}; for(int i=0; i<3; i++) { for(int j=0; j<2; j++) { System.out.print(a[i][j]+" "); } System.out.println(); } } } |
Output
2 3
4 5
Initialize 2D array of variable columns length
If we want to create an array that contains variable length variable length columns then use the below approach and initialize it using the for loop. See the example below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
public class Main { public static void main(String args[]) { int count = 0; // Creating 2D array int a[][] = new int[3][]; // Setting variable column a[0] = new int[3]; a[1] = new int[4]; a[2] = new int[5]; for(int i = 0; i<a.length; i++) { for (int j = 0; j < a[i].length; j++) { a[i][j] = count++; System.out.print(a[i][j]+" "); } System.out.println(); } } } |
Output
3 4 5 6
7 8 9 10 11
Initialize 2D array with heterogeneous data
Although array is used to collect similar type of data but we can store different type of values as well. See the example below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
public class Main { public static void main(String args[]) { int count = 0; // Creating 2D array Object a[][] = new Object[3][]; // Setting variable column a[0] = new Integer[3]; a[1] = new String[4]; a[2] = new Float[5]; for(int i = 0; i<a.length; i++) { for (int j = 0; j < a[i].length; j++) { if(i == 1) a[i][j] = "A"; else if(i == 2) a[i][j] = (float)count; else a[i][j] = count++; System.out.print(a[i][j]+" "); } System.out.println(); } } } |
Output
A A A A
3.0 3.0 3.0 3.0 3.0
Initialize 2D array using initialilzer with columns
If we want to initialize a variable lenght columns array at the same time of creating an array then use initialilzer block as we did in the given example.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
public class Main { public static void main(String args[]) { int count = 0; // Creating 2D array int a[][] = new int[3][]; // Initializing each a[0] = new int[] { 1,2,3 }; a[1] = new int[] { 4,5,6,7}; a[2] = new int[] { 8,9,10,11,12 }; for(int i = 0; i<a.length; i++) { for (int j = 0; j < a[i].length; j++) { System.out.print(a[i][j]+" "); } System.out.println(); } } } |
Output
4 5 6 7
8 9 10 11 12
Initialize 2D array using Reflection API
Java provides a class Array in reflection package that can be used to create an array. Here, newInstance()
method is used to create an instance of array which is later initialized in the for loop.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
import java.lang.reflect.Array; public class Main { public static void main(String args[]) { int count = 0; // Creating 2D array int[][] a = (int[][]) Array.newInstance(int.class, 3, 2); for(int i = 0; i<a.length; i++) { for (int j = 0; j < a[i].length; j++) { a[i][j] = count++; System.out.print(a[i][j]+" "); } System.out.println(); } } } |
Output
2 3
4 5
Initialize 2D array using the toCharArray()
If we have a character array and initialize it at the same time then we can use toCharArray()
method of String
class that returns a char array. See the example below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
public class Main { public static void main(String args[]) { // Creating and Initializing 2D array char[][] ch = { "JAVA".toCharArray(), "PYTHON".toCharArray(), "REST".toCharArray() }; for(int i = 0; i<ch.length; i++) { for (int j = 0; j < ch[i].length; j++) { System.out.print(ch[i][j]+" "); } System.out.println(); } } } |
Output:
P Y T H O N
R E S T
That’s all about How to initialize 2D array in java