In this tutorial you will learn about the Java Program to Print 2D array and its application with practical example.
In this tutorial, we will learn to create a Java Program to Print 2D array using Java programming.
Prerequisites
Before starting with this tutorial we assume that you are best aware of the following Java programming topics:
- Java Operators.
- Basic Input and Output function in Java.
- Class and Object in Java.
- Basic Java programming.
- If-else statements in Java.
- For loop in Java.
What is 2D Array[]?
Multidimensional Arrays Or 2D array defined as in simple words as “array of arrays”.
For example:
The array int[][] a = new int[5][5] can store a total of (5*5) = 25 elements.
Java Program to Print 2D array
In this program we would simply print a “2d” array .first of we would declare variables and called a function to display all the value of array[].let have a look at the code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
//Java program to print 2d aaray import java.util.*; class Array2D { public static void Printing_String(int arr[][]) { // printing the array values.. for (int n = 0 ; n < arr.length ; n++) { System.out.println(Arrays.toString(arr[n])); } } // Main function public static void main(String args[]) { //declaring 2d array int arr[][] = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } }; Printing_String(arr); // passing value to function() } } |
Output
In the above program, we have first declared and initialized a set variables required in the program.
- arr[] = it will hold array values.
- n= for iteration
After declaring variables we pass the array to function Printing_String().
where we simply print the array value using for loop.