返回列表 發帖

陣列 (一)

「陣列(Array)」,按序排列的同類資料元素的集合
1.索引值是從0 開始計數
2.索引值不可超出陣列的範圍
3.取陣列最後一個值=陣列長度-1


分別運用四種不同的陣列宣告方式, 完成參考執行畫面.

  1. public class Ch34
  2. {
  3.     public static void main(String args[])
  4.     {
  5.         String a[]={"春","夏","秋","冬"};
  6.         
  7.         String b[]={"甲","乙","丙","丁"};



  8.         System.out.print("陣列a: ");
  9.         for(int i=0; i<4; i++)
  10.             System.out.print(a[i]+" ");
  11.         System.out.println();

  12.         System.out.print("陣列b: ");
  13.         for(int i=0; i<4; i++)
  14.             System.out.print(b[i]+" ");
  15.         System.out.println();
  16.         
  17.         

  18.         int c[]={1,2,3,4};
  19.         
  20.         //string d[4]; -> c++宣告長度為4的空陣列
  21.         String d[]=new String[4];     //宣告長度為4的空陣列
  22.         d[0]="A";
  23.         d[1]="B";
  24.         d[2]="C";
  25.         d[3]="D";

  26.         //foreach
  27.         //for(陣列型態 變數:陣列(來源))
  28.         System.out.print("陣列c: ");

  29.         for(int i:c)
  30.             System.out.print(i+" ");
  31.         System.out.println();
  32.         
  33.         System.out.print("陣列d: ");
  34.         for(String sa:d)
  35.             System.out.print(sa+" ");
  36.         System.out.println();
  37.     }
  38. }
複製代碼

返回列表