Pages

How to Print Array with elements in Java?

You cannot print array elements directly in Java, you need to use Arrays.toString() or Arrays.deepToString() to print array elements. Use toString() if you want to print one-dimensional array and use deepToString() method if you want to print two-dimensional array. Have you tried printing array in Java before? What did you do? just passed an array to println() method and expecting it prints its elements? Me too, but surprisingly array despite being Object and providing a length field, doesn't seem overriding toString() method from java.lang.Object class. All it prints is type@somenumber. This is not at all useful for anyone who is interested in seeing whether an array is empty or not, if not then what elements it has etc.
To print Java array in a meaningful way, you don't need to look further because your very own Collection framework provides lots of array utility methods in java.util.Arrays class. Here we have toString() and deepToString() method to print array in Java.

These methods are overloaded, much like System.out.println() method to accept all primitive types, which means a different method is called if you pass boolean array, and a different one is called when you print integer array.

Same is true with deepToString(), which is used to print two dimensional array in Java. In this Java array tutorial, we will see examples of printing string array, integer array, byte array and a two dimensional array in Java. Rest of them are like that, which means by following these examples, you should be able to print boolean, char, short, float, double and long array by your own.

How to Print int array in Java

In order to print integer array, all you need to do is call Arrays.toString(int array) method and pass your integer array to it. This method will take care of printing content of your integer array, as shown below. If you directly pass int array to System.out.println(), you will only see type of array and a random number.

How to Print byte array in Java

printing byte array is no different than printing int array, as Arrays class provides an overloaded method toString(byte[] bytes) to print contents of byte array in Java, as shown below. By the way, if you want to print byte array as Hex String then see this link.

How to Print String array in Java

Printing string array in Java is probably easiest thing to do, because Arrays class has another overloaded version of toString() to accept Object. This method calls toString() of Object to get a printable String. This can also be used to print array of any arbitrary object in Java. User defined object must override toString() method to show something reasonable on console.

How to Print Two Dimensional array in Java

Arrays class provides a different method to print two dimensional array in Java, it’s called toDeepString(). It's capable of printing multi-dimensional array in Java and similar to toDeepEquals() which is used to compare multi-dimensional array in Java. This method is also overloaded and provides 8 + 1 primitive and object versions to accept boolean, byte, short, char, int, long, float, double and Object in Java. Here is an example of how to print two dimensional array in Java.

Complete Java Program to Print Array in Java

This is the full Java code of print different types of array in Java. As explained in this article, it prints integer, String, byte and two dimensional array using toString() and deepToString() method of java.util.Arrays class. You can copy paste this program in your Java IDE and run it. Don't need of any third-party libraries.

That's all about how to print array in Java. We have learned how to print objects of array, instead of array object, which is nothing but a hashCode. I really hope that Java should add a toString() in Array, instead of providing Arrays.toString(), don't know when they chose other part. Nevertheless, toString() and toDeepString() from java.util.Arrays class is sufficient to print any kind of one dimensional and two dimensional array in Java. Though special care needs to take, while printing byte arrays, which requires byte to be encoded in Hex string.

5 comments:

Hello @samiulla, you can convert byte array to String using new String(). If its coming from other host or encoded in some other encoding other than platform's default, provide that encoding e.g. "UTF-8" to String constructor.

I have a JAXBElement element 'baseData' which has some base64 data in it. Am trying to read this using (new String(baseData.getData().getValue())). But all I get is non-readable characters. If I use just (baseData.getData().getValue()), I get the offset address. Am just lost since two days. Can you please help me with this? I want to read the Base64 data and print it.

I tried to find the method toDeepEquals() but success. there is a method Arrays.deepEquals().Please Update the articles for further/next reader. if i am right.Thanks and RegardsShesh Narayan Chakrapani.