How to print an array in Java?

In Java, you can use a for loop to iterate through an array and use the System.out.println() method to print out the elements of the array. Below is a sample code:

public class ArrayPrintingExample {
    public static void main(String[] args) {
        int[] numbers = {1, 2, 3, 4, 5};

        for (int i = 0; i < numbers.length; i++) {
            System.out.println(numbers[i]);
        }
    }
}

This code segment creates an integer array named numbers, then uses a for loop to iterate through each element in the array and prints out the value of each element using the System.out.println() method. Running this code will print out each element of the array.

1
2
3
4
5
bannerAds