配列の参照

●構文

○2つの文で配列を作成
 型名 配列変数名[添字];
 配列変数名 = new 型名[要素数]

○1つの文で配列を作成
 型名 配列変数名[] = new 型名[要素数];
  

●サンプル

class Sample1 {
  public static void main(String args[]) {
    int test[] = new int[5];  //5つの要素数を持った配列を作成

    test[0] =80;  //配列要素に80を代入
    test[1] =60;  //配列要素に60を代入
    test[2] =22;  //配列要素に22を代入
    test[3] =50;  //配列要素に50を代入
    test[4] =75;  //配列要素に75を代入
    for(int i =0; i<5; i++){
      System.out.println((i+1)+"番目の人の点数は"+test[i]+"です");  //配列の0番目の内容から順に表示
    }
  }
}
  

●実行結果

 1番目の人の点数は80です。
 2番目の人の点数は60です。
 3番目の人の点数は22です。
 4番目の人の点数は50です。
 5番目の人の点数は75です。