How to print the content of array in java

We all know what toString() method does. It returns a string representation of the object. This representation has been made informative for most of the java classes provided in JDK bundle. But still, arrays are one of such exceptions toString() method does not provide any useful information.

Ads by Google

In this post, we will learn about JDK’s existing support to print the arrays in more informative way.

Sections in this post:
Normal way of doing it
Using Arrays class to print array
Printing array of array

Normal way of doing it

Normally, if print array or for that matter any collection, we do it like this:


// A normal java list having four String objects
List<String> list = new ArrayList<String>();
list.add("First");
list.add("Second");
list.add("Third");
list.add("Fourth");
// Print the list in console
System.out.println(list);

Output:
[First, Second, Third, Fourth]

Similarly, many of us forgot about array and follow same thing:

// An array of String objects
String[] array = new String[] { "First", "Second", "Third", "Fourth" };

// Print the array using default toString method
System.out.println(array.toString());

Output:
[Ljava.lang.String;@12dacd1

Using Arrays class to print array

Now, lets see new recommended way of doing this. This involves using Arrays.toString().

// An array of String objects
String[] array = new String[] { "First", "Second", "Third", "Fourth" };

// Print the array using Arrays.toString()
System.out.println(Arrays.toString(array));

Output:
[First, Second, Third, Fourth]

Printing array of array

What will happen if somewhere in hierarchy another array is stored, like in case of array of array. Again same problem. Do not worry, we have another method to rescue. It is Arrays.deepToString(). Lets see hot it works.

String[] arr1 = new String[] { "Fifth", "Sixth" };
String[] arr2 = new String[] { "Seventh", "Eight" };
// An array of array containing String objects
String[][] arrayOfArray = new String[][] { arr1, arr2 };

// Compare the different outputs

// Print the array using default toString method
System.out.println(arrayOfArray);

// Print the array using Arrays.toString()
System.out.println(Arrays.toString(arrayOfArray));

// Print the array using Arrays.deepToString()
System.out.println(Arrays.deepToString(arrayOfArray));

Output:
[[Ljava.lang.String;@1ad086a
[[Ljava.lang.String;@10385c1, [Ljava.lang.String;@42719c]
[[Fifth, Sixth], [Seventh, Eighth]]

In below given code sample, I have compared all above techniques. Feel free to copy and play with it.

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class PrintArray {
	public static void main(String[] args) {

		// A normal java list having four String objects
		List<String> list = new ArrayList<String>();
		list.add("First");
		list.add("Second");
		list.add("Third");
		list.add("Fourth");
		// Print the list in console
		System.out.println(list);

		// An array of String objects
		String[] array = new String[] { "First", "Second", "Third", "Fourth" };
		// Print the array using default toString method
		System.out.println(array.toString());
		// Print the array using Arrays.toString()
		System.out.println(Arrays.toString(array));

		String[] arr1 = new String[] { "Fifth", "Sixth" };
		String[] arr2 = new String[] { "Seventh", "Eigth" };
		// An array of array containing String objects
		String[][] arrayOfArray = new String[][] { arr1, arr2 };

		// Compare the different outputs

		// Print the array using default toString method
		System.out.println(arrayOfArray);

		// Print the array using Arrays.toString()
		System.out.println(Arrays.toString(arrayOfArray));

		// Print the array using Arrays.deepToString()
		System.out.println(Arrays.deepToString(arrayOfArray));
	}
}

Output:

[First, Second, Third, Fourth]
[Ljava.lang.String;@12dacd1
[First, Second, Third, Fourth]
[[Ljava.lang.String;@1ad086a
[[Ljava.lang.String;@10385c1, [Ljava.lang.String;@42719c]
[[Fifth, Sixth], [Seventh, Eigth]]

Happy Learning !!

Ads by Google

2 thoughts on “How to print the content of array in java”

  1. Very good article. Be aware that your posts are getting copied. I recently saw this article content on some other famous Java blog. Please ‘visit’ that ‘java’ blog recent posts.

Note:- In comment box, please put your code inside [java] ... [/java] OR [xml] ... [/xml] tags otherwise it may not appear as intended.

Leave a Reply

Your email address will not be published. Required fields are marked *


one + 8 =

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>