数组

public class TestArray {
    public static void main(String[] args) {
        int i;
        i = 12;
        boolean b = true;
        // 定义一个数组;数组的声明
        String[] names;
        int scores[];
        // 初始化
        // 静态初始化:初始化数组与给数组元素赋值同时进行
        names = new String[] { "王林", "李慕婉", "周佚" };

        // 动态初始化:初始化数组和给数组元素赋值分开进行
        scores = new int[4];
        // 如何调用相应的数组元素:通过数组元素的下角标的方式来调用
        // 下角标从0 开始,到 n-1 结束,其中 n 表示数组的长度
        scores[0] = 87;
        scores[1] = 88;
        scores[3] = 83;
        // 数组的长度:通过数组的 length 属性。
        System.out.println(names.length);// 3
        System.out.println(scores.length);// 4
        // 如何遍历数组元素
        for (int j = 0; j < names.length; j++) {
            System.out.println(names[j]);
        }
    }
}

注意:数组一旦初始化,其长度是不可变

All that work will definitely pay off
原文地址:https://www.cnblogs.com/afangfang/p/12454816.html