How to print array in java

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.

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

Output:

[1, 6, 7, 9, 4]

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.

Output:

[[1, 2, 3], [4, 5, 6], [7, 8, 9]]

Using Java 8 Stream API

In java 8, you can convert array to Stream and print array with Stream’s foreach() method.

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.

Output:

1 6 7 9 4
3rd element of array is: 7

Using for-each loop

For each works same as for loop but with differs syntax.

Output:

1 6 7 9 4

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.

Output:

4 9 7 6 1

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

Create main class named PrintArrayOfColors.java

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.

When you run PrintListOfColorsMain again, you will get below output:

[Color{name=’Red’, colorCode=’#FF0000′}, Color{name=’Blue’, colorCode=’0000FF’}, Color{name=’White’, colorCode=’#FFFFFF’}, Color{name=’Green’, colorCode=’#008000′}]

As you can see, we have much meaningful output now.

That’s all about how to print array in java.

Was this post helpful?

Related Posts

  • 17 May

    How to Initialize an Array with 0 in Java

    Table of ContentsArraysInitializing Newly Created ArrayUsing Default Initialization of Arrays in JavaUsing Initialization After DeclarationUsing Simultaneous Initializing and Declaration of ArrayUsing Reflection in JavaInitializing Existing ArrayUsing the Arrays.fill() Function of JavaUsing the for LoopUsing Reassignment From Another ArrayUsing Collections.nCopies() Function of JavaInitializing User-Defined Object ArrayConclusionWas this post helpful? This article discusses the arrays and different […]

  • Set an array to another array in java
    23 April

    Set an Array Equal to Another Array in Java

    Table of ContentsSetting an Array Variable Equal to Another Array VariableSet an Array Equal to Another Array in Java Using the clone() MethodSet an Array Equal to Another Array in Java Using the arraycopy() MethodSet an Array Equal to Another Array in Java Using the copyOf() MethodSet an Array Equal to Another Array in Java […]

  • Check if array is empty in Java
    04 March

    Check if Array Is Empty in Java

    Table of ContentsCheck if the Array Is Empty in JavaThe Array Variable Has the Null ReferenceThe Array Does Not Contain Any ElementThe Array Has Only Null ElementsUsing the Java Library to Check if the Array Is Empty in JavaUsing Apache Commons Library to Check if the Array Is Empty in JavaConclusionWas this post helpful? In […]

  • Initialize empty array in java
    01 January

    Initialize empty array in java

    Table of ContentsIntroductionHow do you initialize an empty array in java?Using new Keyword with predefined Values and SizeUsing Anonymous Array Objects to initialize empty arrayUsing java.util.Scanner Class for user input with predefined size. Using java.io.BufferedReader to initialize array for user input with unknown sizeUsing fill() method of java.util.Arrays Class to initialize empty arrayWas this post helpful? […]

  • Write a Program to Find the Maximum Difference between Two Adjacent Numbers in an Array of Positive Integers
    07 April

    Write a Program to Find the Maximum Difference between Two Adjacent Numbers in an Array of Positive Integers

    In this article, we look at a problem : Given an Array of Positive Integers, Find the [Maximum Difference](https://java2blog.com/maximum-difference-between-two-elements-in-array/ "Maximum Difference") between Two Adjacent Numbers. For each pair of elements we need to compute their difference and find the Maximum value of all the differences in array. Let us look at an example, Consider this […]

  • 09 October

    Initialize 2D array in Java

    Table of ContentsInitialize 2D array Using for loopInitialize 2D array using an initializerInitialize 2D array of variable columns lengthInitialize 2D array with heterogeneous dataInitialize 2D array using initialilzer with columnsInitialize 2D array using Reflection APIInitialize 2D array using the toCharArray()Was this post helpful? In this article, we will learn to initialize 2D array in Java. […]

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.