How to Print array in Java using Arrays

Connect with

Oracle Java
How to print array either int or string or any another type is straight forward by using Arrays java utility class.

Using Arrays.toString and Arrays.deepToString

You should choose utility class or library, if there is not compelling reason to chose your custom approach. You know performance of library or utility will be good comparatively your custom approach most of the time.

  • Use java utility method toString() from utility class java.util.Arrays,if arrays is single dimensional and
  • Use java utility method deepToString() from utility class java.util.Arrays,if arrays is mulit-dimensional .

package com.mysoftkey.java.util;

import java.util.Arrays;

/**
 * This java class is used to display my custome int array or string array Using
 * Arrays.toString() and Arrays.deepToString utility methods.
 * 
 * 
 * @author Ranjeet Jha
 *
 */
public class PrintMyArrayExample {
  public static void main(String[] args) {
		
       int[] myIntAray = { 1, 8, 5, 2, 9 };
	System.out.println("Using Arrays.toString() method : ");
	System.out.println(Arrays.toString(myIntAray));

	/*
	 *  if use simple Arrays.toString() method on multi dimensional ,
	 *  you get address of reference, not value, 
	 *  you have to use Arrays.deepToString() method to print nested array.  
	 */
	int[][] deepArrayInt = new int[][] { { 1, 3 }, { 29, 40, 878 } };
	System.out.println(" \n Using Arrays.toString() method on multidimensional array: ");
	System.out.println(Arrays.toString(deepArrayInt));

	System.out.println(" \n Using Arrays.deepToString() method on multi-dimensional array: ");
	System.out.println(Arrays.deepToString(deepArrayInt));

	/*
	 * now we have taken String[] array with single and multi-dimensional 
	 */
	String[] myStrArray = { "Java", "Spring", "Hibernate" };
	System.out.println("\n Using Arrays.toString() method: ");
	System.out.println(Arrays.toString(myStrArray));



	// 2d or 2 dimensional array, using Arrays.deepToString
	String[][] deepArrayStr = new String[][] { { "java", "j2ee" }, { "spring", "hibernate" } };
	System.out.println(" \n Using Arrays.toString() on multi-dimensional array, which print object reference: ");
	System.out.println(Arrays.toString(deepArrayStr));

	System.out.println(" \n Using Arrays.deepToString() method on multi-dimensional array: ");
	System.out.println(Arrays.deepToString(deepArrayStr));


 }
}

Program Output

Using Arrays.toString() method : 
[1, 8, 5, 2, 9]
 
 Using Arrays.toString() method on multidimensional array: 
[[I@15db9742, [I@6d06d69c]
 
 Using Arrays.deepToString() method on multi-dimensional array: 
[[1, 3], [29, 40, 878]]

 Using Arrays.toString() method: 
[Java, Spring, Hibernate]
 
 Using Arrays.toString() on multi-dimensional array, which print object reference: 
[[Ljava.lang.String;@4e25154f, [Ljava.lang.String;@70dea4e]
 
 Using Arrays.deepToString() method on multi-dimensional array: 
[[java, j2ee], [spring, hibernate]]

Please write you suggestion/comment to improve this post.


Connect with

2 thoughts on “How to Print array in Java using Arrays

  1. Pingback: Michal
  2. Pingback: sweety

Leave a Reply

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