[Java] How to print an array in human readable format?


I want to print an array in human readable format to console, how can I do that Java?

1 Answer

3 years ago by

If we directly try to print the array, Java prints the reference of that array, which will not be useful. We can use Arrays.toString to print the array in human readable format.

import java.util.Arrays;

public class JavaArrays {
	public static void main(String[] args) {
		String[] colors = { "blue", "red", "green", "yellow" };

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

	}
}

Output:

[Ljava.lang.String;@1dbd16a6
[blue, red, green, yellow]

Try this code online here: https://onecompiler.com/java/3xmtc899a

3 years ago by Karthik Divi