OneCompiler

[Java] How to print an array?

I want to print all elements from an array to console, how to do that in Java?

1 Answer

4 years ago by

We can use Arrays.toString util method to print an array. Following code shows how to use that.

import java.util.Arrays;

public class JavaArrays {
	public static void main(String[] args) {
		String[] colors = { "blue", "red", "green", "yellow" };
		
		System.out.println(Arrays.toString(colors));
	}
}

Output:

[blue, red, green, yellow]

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

4 years ago by Karthik Divi