×
配列
配列の宣言、インスタンス化、初期化、要素へのアクセスを示す基本的なコード例です。
public class ArrayExample {
    public static void main(String[] args) {
        // 宣言と領域確保 (デフォルト値 0 で初期化される)
        int[] numbers = new int[3];
        numbers[0] = 10;
        numbers[1] = 20;
        numbers[2] = 30;

        // 配列リテラルによる初期化
        String[] colors = {"Red", "Green", "Blue"};

        System.out.println("要素数: " + colors.length); // 3
        System.out.println("最初の要素: " + colors[0]);  // Red

        // numbers[3] = 40; // 実行時例外 ArrayIndexOutOfBoundsException!
    }
}

もう少し詳しく