打印数组内容

for

public class Main {
    public static void main(String[] args) {
        int[] ns = { 1, 4, 9, 16, 25 };
        for (int i=0; i<ns.length; i++) {
            int n = ns[i];
            System.out.println(n);
        }
    }
}

for each

public class Main {
    public static void main(String[] args) {
        int[] ns = { 1, 4, 9, 16, 25 };
        for (int n : ns) {
            System.out.println(n);
        }
    }
}

Arrays.toString()

import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        int[] ns = { 1, 1, 2, 3, 5, 8 };
        System.out.println(Arrays.toString(ns));
    }
}
THE END
最后修改:2020 年 03 月 05 日 15 : 10
本文链接:https://www.j000e.com/%E6%95%B0%E7%BB%84%E6%93%8D%E4%BD%9C/Iterate.html
版权声明:本文『[遍历数组][Arrays.toString()]』为『Joe』原创。著作权归作者所有。
转载说明:[遍历数组][Arrays.toString()] || Joe's Blog』转载许可类型见文末右下角标识。允许规范转载时,转载文章需注明原文出处及地址。
Last modification:March 5, 2020
If you think my article is useful to you, please feel free to appreciate