What's the simplest way to print a Java array?

There are several ways to print an array in Java. Here are a few options:

  1. Using a loop:
int[] array = {1, 2, 3, 4, 5};

for (int i = 0; i < array.length; i++) {
    System.out.print(array[i] + " ");
}
  1. Using the Arrays.toString() method:
import java.util.Arrays;

int[] array = {1, 2, 3, 4, 5};

System.out.println(Arrays.toString(array));
  1. Using the String.join() method (Java 8 or later):
int[] array = {1, 2, 3, 4, 5};

System.out.println(String.join(" ", Arrays.stream(array).mapToObj(String::valueOf).toArray(String[]::new)));

Each of these methods will print the elements of the array on a single line, separated by a space.