In this post, we will see how to print array in java.
There are multiple ways to print an array. Let’s go through few ways to print array in java.
Table of Contents
Using Arrays.toString()
You can use Arrays.toString()
method to print array in java. This is simplest ways to print an array.
Arrays.toString() returns string object.This String contains array in string format.
Let us understand with the code
1 2 3 4 5 6 7 8 9 10 11 |
import java.util.*; public class Main { public static void main(String args[]) { int a[]={1,6,7,9,4}; int n = a.length; //a.length returns size of array. String f = Arrays.toString(a); //Array in string format System.out.println(f); } } |
Output:
Using deepToString() method
Basically Arrays.toString()
works fine for a 1-dimensional arrays,but it won’t work for multidimensional arrays.
Let’s say we have 2-dimensional array as shown
1 2 3
4 5 6
7 8 9
To convert multidimensional arrays to string, you can use Arrays.deepToString()
method. This is how you can print 2D array in java.
1 2 3 4 5 6 7 8 9 10 11 |
import java.util.*; public class Main { public static void main(String args[]) { int a[][] = {{1,2,3},{4,5,6},{7,8,9}}; String x = Arrays.deepToString(a); System.out.println(x); } } |
Output:
Using Java 8 Stream API
In java 8, you can convert array to Stream and print array with Stream’s foreach() method.
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 28 29 30 31 32 33 34 35 36 |
package org.arpit.java2blog; import java.util.Arrays; public class PrintArrayMain { public static void main(String args[]) { System.out.println("================"); System.out.println("Printing 1D String array"); System.out.println("================"); String[] countriesArr = new String[]{"India", "China", "Japan", "Bhutan"}; Arrays.stream(countriesArr).forEach(System.out::println); System.out.println("================"); System.out.println("Printing 1D Integer array"); System.out.println("================"); int[] arrayInt = {1, 2, 3, 4, 5}; Arrays.stream(arrayInt).forEach(System.out::println); System.out.println("================"); System.out.println("Printing 2D String array"); System.out.println("================"); String[][] string2DArray = new String[][]{{"Java", "Python"}, {"Go", "Kotlon"}}; Arrays.stream(string2DArray).flatMap(str -> Arrays.stream(str)).forEach(System.out::println); System.out.println("================"); System.out.println("Printing 2D String array"); System.out.println("================"); int[][] int2DArray = new int[][]{{10, 20, 30, 40}, {50, 60, 70, 80}}; Arrays.stream(int2DArray).flatMapToInt(i -> Arrays.stream(i)).forEach(System.out::println); } } |
Output:
Printing 1D String array
================
India
China
Japan
Bhutan
================
Printing 1D Integer array
================
1
2
3
4
5
================
Printing 2D String array
================
Java
Python
Go
Kotlon
================
Printing 2D String array
================
10
20
30
40
50
60
70
80
As you can see we have seen how to print String array and Integer array in java using Stream.
Using for loop
You can iterate the array using for loop and print elements of array in java.
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[]) { int a[]={1,6,7,9,4}; int n = a.length; //a.length returns size of array. for(int i=0;i<n;i++) { System.out.print(a[i]+" "); //printing all the elements of array. } System.out.println(); //Arrays can be accessed randomly //Eg : Lets try to print 3rd element of array, System.out.println("3rd element of array is: "+a[2]); } } |
Output:
3rd element of array is: 7
Using for-each loop
For each works same as for loop but with differs syntax.
1 2 3 4 5 6 7 8 9 10 11 |
public class Main { public static void main(String args[]) { int a[]={1,6,7,9,4}; for(Integer i : a) //for every iteration, each array element is stored in i. { System.out.print(i+" "); //printing all the elements of array. } } } |
Output:
Print array in reverse order in java
You can iterate the array using for loop in reverse order and print the element. This will print array in reverse order.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
public class Main { public static void main(String args[]) { int a[]={1,6,7,9,4}; int n = a.length; //a.length returns size of array. for(int i=n-1;i>=0;i--) { System.out.print(a[i]+" "); //printing all the elements of array. } System.out.println(); } } |
Output:
Print array with custom objects
In case, you have custom object in the array, then you should override toString()
method in custom class, else it will give you unexpected results.
Let’s see with the help of example
Create a simple class named Color.java
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 28 29 30 31 |
package org.arpit.java2blog; public class Color { String name; String colorCode; public Color(String name, String colorCode) { super(); this.name = name; this.colorCode = colorCode; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getColorCode() { return colorCode; } public void setColorCode(String colorCode) { this.colorCode = colorCode; } } |
Create main class named PrintArrayOfColors.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
package org.arpit.java2blog; import java.util.Arrays; public class PrintArrayOfColors { public static void main(String[] args) { Color red = new Color("Red", "#FF0000"); Color blue = new Color("Blue", "0000FF"); Color white = new Color("White", "#FFFFFF"); Color green = new Color("Green", "#008000"); Color[] colorArray=new Color[]{red,blue,white,green}; System.out.println(Arrays.toString(colorArray)); } } |
Output:
If you notice, we don’t have toString()
method in Color
class, that’s why it is calling Object’s toString() method and we are not able to see meaningful results.
We need to override toString()
method in Color
class and we will get informative output.
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 28 29 30 31 32 33 34 35 36 37 38 39 |
package org.arpit.java2blog; public class Color { String name; String colorCode; public Color(String name, String colorCode) { super(); this.name = name; this.colorCode = colorCode; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getColorCode() { return colorCode; } public void setColorCode(String colorCode) { this.colorCode = colorCode; } @Override public String toString() { return "Color{" + "name='" + name + '\'' + ", colorCode='" + colorCode + '\'' + '}'; } } |
When you run PrintListOfColorsMain
again, you will get below output:
As you can see, we have much meaningful output now.
That’s all about how to print array in java.