When we want to store elements for a similar type in Java, we take the name of Array. Array stores elements of similar type viz: integer, string, etc. Moreover, the dimension of the array also varies in Java according to your requirements. Therefore, whenever you have data in a 2D array, you need to print those elements.
There are different types of techniques, those you can choose for printing the elements of a two-dimensional array in java. You can use any of those techniques for printing the elements of a two-dimensional array.
ADVERTISEMENT Popular Course in this category JAVA MASTERY - Specialization | 78 Course Series | 15 Mock TestsAll of the techniques, which I will be showing here, have associated examples of codes. Those will provide you with better insights and practical hands-on. You should see carefully the codes that have inline comments to provide better readability. I have also provided outputs in the form of screenshots at the end of each code.
Let us see the techniques one by one. We will start with the syntax first.
In the case of the 2D array, the values are stored in a matrix format, which means it is based on row and column index. You can follow any of the below syntaxes for the declaration of an array in java.
Syntax:
dataType[][] reference_variable name; dataType [][]reference_variable name; dataType reference_variable name [][]; dataType []reference_variable name [];
You can follow the below example to create an instance of a two-dimensional array in java of integer type; the number of rows and columns here is 5.
int[][] matrx=new int[5][5];
You can follow the below example for the initialization of a 2D Array in Java.
matrx [0][0]=51; matrx [0][1]=62; matrx [0][2]=73; matrx [1][0]=84; matrx [1][1]=95; matrx [1][2]=46; matrx [2][0]=37; matrx [2][1]=18; matrx [2][2]=29;
Below are some examples of how to print a 2d array in java:
In the below example, we will show an example of how to print an array of integers in java.
Code:
public class Print2DArrayInJava { public static void main(String[] args) { //below is declaration and intialisation of a 2D array final int[][] matrx = { { 11, 22}, { 41, 52}, }; for (int r = 0; r < matrx.length; r++) { //for loop for row iteration. for (int c = 0; c < matrx[r].length; c++) { //for loop for column iteration. System.out.print(matrx[r][c] + " "); } System.out.println(); //using this for new line to print array in matrix format. } } }
Output:
When a number of rows and columns are not equal in a multidimensional array in java, we call it a Jagged array in Java. Here the number of columns differs from that of its rows.
In the below example, we will show an example of how to print a jagged array of integers in java.
Code:
public class PrintJaggedArrayInJava { public static void main(String[] args) { //below is declaration of a jagged 2 D array int matrx[][] = new int[2][]; matrx[0] = new int[3]; matrx[1] = new int[5]; //initiatiation of a jagged 2 D array int cnt = 1; for (int r=0; r<matrx.length; r++) for(int c=0; c<matrx[r].length; c++) matrx[r][c] = cnt++; //printing of a jagged 2 D array for (int r=0; r<matrx.length; r++){ for (int c=0; c<matrx[r].length; c++){ System.out.print(matrx[r][c]+" "); } System.out.println();//printing of new line } } }
Output:
In the below example, we will show an example of how to print a 2D array of strings in java.
Code:
public class Print2DArrayInJava { public static void main(String[] args) { //below is declaration and intialisation of a 2D array of strings String[][] matrx = {{"OnePlus", "Pixel"}, {"Apple", "Oppo"}}; for (int r = 0; r < matrx.length; r++) { //for loop for row iteration. for (int c = 0; c < matrx[r].length; c++) { //for loop for column iteration. System.out.print(matrx[r][c] + " "); } System.out.println(); //using this for new line to print a 2D array of strings in matrix format. } } }
Output:
Methods for printing 2d arrays in java are explained below:
For loop is used for iteration, and hence we will use for loop to iterate elements of an array in java.
Code:
public class PrintUsingForLoop { public static void main(String[] args) { final int[][] ar = { { 5, 9 }, { 2, 4 } }; for (int r = 0; r < ar.length; r++) { //for loop for row iteration. for (int c = 0; c < ar[r].length; c++) { //for loop for column iteration. System.out.print(ar[r][c] + " "); } System.out.println(); } } }
Output:
To traverse through an array, we can also use the for-each loop.
Code:
import java.util.*; public class PrintUsingForEachLoop { public static void main(String[] args) { int[][] rec = new int[2][2]; rec[0][0] = 15; rec[0][1] = 25; rec[1][0] = 35; rec[1][1] = 45; for(int[] rc: rec){ //using for each loop System.out.println(Arrays.toString(rc)); } } }
Output:
Here we will use Arrays.deepToString() method of java.util.Arrays package does a deep conversion into a string of an array. To use this method, we need to import the package java.util.Arrays.
Code:
import java.util.*; //import package public class PrintUsingDeepToString { public static void main(String[] args) { int[][] rec = {{37, 57}, {47, 58}}; System.out.println(Arrays.deepToString(rec)); //deep conversion } }
Output:
In the above output, the levels of square brackets denote the dimension of the array, which is 2.
So, in this module, we have learned about different techniques on how to print 2 D array in Java. This is nothing new if we compare it to print single dimensional array, except the fact that you need to modify in the declaration, initialization and printing part of the code carefully. If you are a master in a single-dimensional array, this will be just an extension of that. I have also put code examples. You need to exercise those and tally your output with the given outputs. Hands-on by yourself is a must in order to master coding.
The above is the detailed content of Print 2D Array in Java. For more information, please follow other related articles on the PHP Chinese website!