Declare and Initialize 2d Array in Java
In this post, we are going to look at how to declare and initialize the 2d array in Java.
Each element in the primitive two-dimensional array gets their respective default values, whereas object array gets null value.
Program to Declare 2d Array
In the below program, we will look at the various ways to declare a two-dimensional array.
public class DeclareDemo { public static void main(String[] args) { int[][] a = new int[3][2]; // declare a two dimensional array with 3 // rows and 2 columns System.out.println("Element [0][0] of int array is : " + a[0][0]); String[][] b = new String[3][3]; // three rows and three columns System.out.println("Element [0,0] of String array is : " + b[0][0]); int[][] c = new int[3][]; // only first dimension is defined System.out.println("[1] row is : " + c[1]); // int[][] d = new [][]; // this is incorrect as no dimension is defined } }
Output:
Element [0][0] of int array is : 0 Element [0,0] of String array is : null [1] row is : null
Explanation:
In the above program, we saw the ways to declare primitive, String array. We also saw how to declare an array with only one dimension.
It is invalid where we do not specify any dimension.
Initialize two-dimensional Array
public class InitializeDemo { public static void main(String[] args) { byte[][] a = new byte[3][2]; short[][] b = new short[2][1]; int[][] c = new int[3][3]; float[][] e = new float[2][2]; double[][] f = new double[3][2]; char[][] g = new char[2][2]; boolean[][] h = new boolean[4][2]; System.out.println("Default value of primitive types"); System.out.println("byte : " + a[0][0]); System.out.println("short : " + b[0][0]); System.out.println("int : " + c[0][0]); System.out.println("float : " + e[0][0]); System.out.println("double : " + f[0][0]); System.out.println("char : " + (int) g[0][0]); System.out.println("boolean : " + h[0][0]); String[][] i = new String[3][2]; System.out.println("Default value of object type"); System.out.println("String : " + i[0][0]); } }
Output:
Default value of primitive types byte : 0 short : 0 int : 0 float : 0.0 double : 0.0 char : 0 boolean : false Default value of object type String : null
Explanation:
We saw how to initialize the primitive and object types of the two-dimensional array.
All the numeric data types either get 0 or 0.0, char gets 0, and String gets null.
Example 2:
public class DCDemo { public static void main(String[] args) { // initialize using loop int[][] matrix = new int[3][3]; for (int i = 0; i < matrix.length; i++) { for (int j = 0; j < matrix[i].length; j++) { matrix[i][j] = i; } } // initialize during creation String[][] languages = { { "Java", "C" }, { "C++", "Fortran" } }; System.out.println("Length is : " + languages.length); } }
Explanation:
Here we initialized the 2d array using for loop and during creation time.
That’s all for this article. By now, you should have a good understanding of how to declare and initialize the 2d array in Java.
Please share if you find this article helpful.