The simplest way to print a Java array is by using Arrays.toString()
for a one-dimensional array or Arrays.deepToString()
for a multi-dimensional array. First, import java.util.Arrays
. Then, pass the array as an argument to these methods. Here’s an example:
java
import java.util.Arrays;
public class ArrayPrinter {
public static void main(String[] args) {
int[] numbers = {1, 2, 3, 4};
System.out.println(Arrays.toString(numbers));
int[][] multiArray = {{1, 2}, {3, 4}};
System.out.println(Arrays.deepToString(multiArray));
}
}
These methods handle formatting for you, making array printing quick and straightforward.