Signup/Sign In
Ask Question
Not satisfied by the Answer? Still looking for a better solution?

What is the most straightforward approach to print a java array?

In Java, array don't abrogate toString(), so in the event that you attempt to print one straightforwardly, you get the className + '@' + the hex of the hashCode of the array, as characterized by Object.toString():
int[] intArray = new int[] {1, 2, 3, 4, 5};
System.out.println(intArray); // prints something like '[I@3343c8b3'

Be that as it may, normally, we'd really need something more like [1, 2, 3, 4, 5]. What's the least complex method of doing that? Here are some example of inputs and outputs:
// Array of primitives:
int[] intArray = new int[] {1, 2, 3, 4, 5};
//output: [1, 2, 3, 4, 5]

// Array of object references:
String[] strArray = new String[] {"John", "Mary", "Bob"};
//output: [John, Mary, Bob]
by

2 Answers

aashaykumar
Always check the standard libraries first.

import java.util.Arrays;

Then try:

System.out.println(Arrays.toString(array));

or if your array contains other arrays as elements:

System.out.println(Arrays.deepToString(array));
sandhya6gczb
Always check the standard libraries first.

 import java.util.Arrays; 

Then try:

 System.out.println(Arrays.toString(array)); 

or if your array contains other arrays as elements:

 System.out.println(Arrays.deepToString(array)); 

Login / Signup to Answer the Question.