溫馨提示×

Java怎么輸出數組中的全部元素

小億
179
2024-02-20 09:17:37
欄目: 編程語言

在Java中,可以使用循環來遍歷數組中的所有元素,并輸出它們。以下是一個簡單的示例:

public class Main {
    public static void main(String[] args) {
        int[] arr = {1, 2, 3, 4, 5};
        
        // 使用for循環輸出數組中的所有元素
        for (int i = 0; i < arr.length; i++) {
            System.out.println(arr[i]);
        }
    }
}

在這個示例中,我們定義了一個包含5個整數的數組arr,然后使用for循環遍歷數組中的所有元素,并使用System.out.println()方法將它們輸出到控制臺。運行以上代碼將輸出:

1
2
3
4
5

0
亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女