How to Print String Arrays in Java

In Java, you can use a loop to iterate through a string array and print out each element in the array. Here is an example code:

public class Main {
    public static void main(String[] args) {
        String[] array = {"Hello", "World", "Java"};

        // 使用for循环打印数组内容
        for (int i = 0; i < array.length; i++) {
            System.out.println(array[i]);
        }

        // 使用增强型for循环打印数组内容
        for (String str : array) {
            System.out.println(str);
        }
    }
}

The code will produce the following output:

Hello
World
Java

Both for loops and enhanced for loops can be used to print the contents of a string array, the choice between the two depends on personal preference and habit.

bannerAds