Printing array values in Java or values of array element in Java would have been much easier if arrays are allowed to directly prints its values whenever used inside System.out.println() or format and printf method, Similar to various classes in Java do this by overriding toString() method. Despite being an object, array in Java doesn't print any meaningful representation of its content when passed to System.out.println() or any other print methods. If you are using array in method argument or any other prominent place in code and actually interested in values of array then you don't have much choice than for loop until Java 1.4. Things has been changed since Java 5 because it introduced two extremely convenient methods for printing values of both primitive and object arrays in Java. Arrays.toString(array) and Arrays.deepToString(twoDimensionArray) can print values of any array. Main difference between Arrays.toString() and Arrays.deepToString is that deepToString is used to print values of multidimensional array which is far more convenient than nesting of multiple for loops. In this Java tutorial we will see 3 different ways of printing array values in Java or value of element from Array in Java.
3 ways to print array values in Java
As I said there is no direct way to print values of array in Java if you directly pass primitive or object array to System.out.println() you will receive following output:
You can't decipher anything until you are quite familiar of this array format and even then it doesn't tell anything about contents of array. It just print type of element and hashcode. In order to print values of array you can use any of following 3 examples:
1) Use enhanced for loop or classic for loop with lenth of array.
2) Use Arrays.asList() to convert Array into ArrayList and than print
3) Use Java 5 Arrays.toString() and Arrays.deepToString() methods
Print Array Value Example 1: Using for loop
for loop is the classical way of printing or displaying values of both one dimension and multidimensional arrays in Java. before Java 5 you can use array.length to iterate over all array elements and printing values for each of them. From Java 5 onwards you can use much cleaner enhanced for loop which doesn't require any counter from moving one element to other in Java. Enhanced for loop in Java 5 is added with other popular language feature e.g. Enum, Autoboxing and Generics. Here is sample code example to print value of element from array using classical and enhanced for loop in Java:
As you see using enhanced for loop for printing array values is more concise and clean.
Print Array Values Example 2: Using Arrays.asList
Arrays.asList() method is used to convert Array into ArrayList and as you know Collection classes overrides toString method to print there contents. By converting array into List we can leverage that property and print values from ArrayList instead of Array. Only limitation of this approach is it doesn't print contents of array if array is of primitive type like int, float or double but works well if Array contains objects like String. Arrays.asList() is also used to create and initialize List in one line. By the way here is simple code example of displaying values form array in Java using Arrays.asList() method:
Print Array Value Example 3: using Arrays.toString and Arrays.deepToString
This is by far best and recommended way of printing values from Array in Java. Only caveat is that Arrays.toString() and Arrays.deepToString() are added from Java 5 onwards along with other features e.g. Generics, varargs or static import. Use Arrays.toString() method to print both primitive and object single or one dimension array and use Arrays.deepToString() method to print values from two dimensional or multidimensional array (array of array in Java). Here is a simple example of printing array values using Arrays.toString() and Arrays.deepToString() in Java:
Java program to print array values
Here is the combined examples of printing value of elements from Array in Java using above three examples. Best way to print elements of Array is to use new methods added in java.util.Arrays class in Java 5 e.g. toString() and deepToString().
That's all on how to print array values in Java. we have seen three different example to display contents of array in Java and most easy and convenient approach is using Arrays.toString() and Arrays.deepToString(). if you are using Java 5.
Related Java Collection tutorials from Javarevisited Blog