1 / 24

陣列 (Array)

陣列 (Array). 陣列的基本認知. 使用 [] 中括號 ,代表陣列。 1 對 [] 代表一維陣列, 2 對 [] 代表二維陣列,以此類推。 配置 相同資料型態 且 連續的記憶體空間 。 存取陣列元素使用「索引值 (index) 」。 陣列索引值的編號從 0 開始。 搭配迴圈使用。. 使用陣列的方法. 使用陣列有以下兩個步驟: 宣告陣列 配置記憶體空間給該陣列. 一維陣列的宣告與配置記憶體. 資料型態 陣列名稱 []; // 1. 宣告 一維陣列 陣列名稱 = new 資料型態 [ 個數 ]; // 2. 配置 記憶體給陣列. 範例說明.

cecile
Download Presentation

陣列 (Array)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 陣列(Array)

  2. 陣列的基本認知 • 使用[]中括號,代表陣列。1對[]代表一維陣列,2對[]代表二維陣列,以此類推。 • 配置相同資料型態且連續的記憶體空間。 • 存取陣列元素使用「索引值(index)」。 • 陣列索引值的編號從0開始。 • 搭配迴圈使用。

  3. 使用陣列的方法 • 使用陣列有以下兩個步驟: • 宣告陣列 • 配置記憶體空間給該陣列 一維陣列的宣告與配置記憶體 資料型態 陣列名稱[]; // 1.宣告一維陣列 陣列名稱 = new資料型態[個數]; // 2.配置記憶體給陣列

  4. 範例說明 • 步驟1 int score[ ]; //宣告整數陣列score

  5. 步驟2 score=new int[4]; //配置4個整數的記憶體空間 陣列是屬於非基本資料型態,因此score儲存的是陣列實體的參考位址

  6. 陣列宣告的多樣寫法 • 宣告score陣列 int score[ ]; //宣告score陣列為整數型態 int[ ] score; //宣告score變數為整數陣列

  7. 一維陣列簡化的寫法 • 一般寫法(投影片3) • 簡化寫法 一維陣列的宣告與配置記憶體 資料型態 陣列名稱[]; // 1.宣告一維陣列 陣列名稱 = new 資料型態[個數]; // 2.配置記憶體給陣列 宣告陣列的同時便配置記憶體 • 資料型態 陣列名稱[]=new 資料型態[個數];

  8. 範例 public class Sample { public static void main(String args[]) { intscore[]; score=new int[4]; score[0]=50; score[1]=60; for(inti=0;i<4;i++) System.out.printf("score[%d]=%d\n",i,score[i]); } }

  9. 練習-TQC 108 • TQC 108: 九九乘法表 • 題目要求使用陣列及迴圈

  10. 練習-TQC 104 • TQC 104: 河洛之數 • 使用Math.random亂數,迴圈。 • 一維陣列模擬二維使用

  11. 陣列初始值設定 • 在宣告時就給予陣列初值 • 給予初值時,依初值的個數來決定陣列的大小。 int score[ ]={50,60,80,75,10} //陣列大小為5,即為score[0]=50, score[1]=60, score[2]=80, score[3]=75, score[4]=10 陣列初值的設定 資料型態 陣列名稱[]={初值1,初值2,…,初值n};

  12. 範例 public class Sample { public static void main(String args[]) { int score[]={50,60,80,75,10}; int sum=0; for(inti=0;i<5;i++) sum+=score[i]; System.out.println("Average="+(float)sum/5); } }

  13. 練習 • 撰寫Java程式,讓使用者 輸入5個成績,加總後算出平均。 • 撰寫Java程式,讓使用者 輸入5個成績,輸出最高與最低的成績。

  14. 取陣列長度的方法(method) • 取得陣列元素的個數(陣列長度)的格式 陣列長度的取得 陣列名稱.length

  15. 續(p.10)範例 public class Sample { public static void main(String args[]) { int score[]={50,60,80,75,10}; int sum=0; for(inti=0;i<score.length;i++) sum+=score[i]; System.out.println("Average="+(float)sum/5); } }

  16. 練習-TQC 204 • TQC 204: 期末考分數計算 • 使用引數,注意總平均求至小數第2位。

  17. 二維陣列 • 宣告與配置記憶體空間 • 簡化寫法 二維陣列的宣告格式 資料型態 陣列名稱[][]; 陣列名稱=new資料型態[列的個數][行的個數]; 二維陣列的宣告格式 資料型態 陣列名稱[][]=new資料型態[列的個數][行的個數];

  18. 二維陣列初值設定 • 在宣告時就給予陣列初值 二維陣列初值的設定格式 資料型態 陣列名稱[][]={{ 第1列初值 }, { 第2列初值 }, { … }, { 第n列初值 }};

  19. 範例 • 某汽車公司的車輛銷售表 • 利用二維陣列儲存 int sale[2][4];

  20. 範例 • 宣告及初值設定 int sale[][]={{32,35,26,30},{34,30,33,31}};

  21. 每列元素個數不同的二維陣列 • 每列元素個數不同的二維陣列 int x[][]={ {25,31,12}, {41,74,10,32,65}, {82,57} }; int x[][]=new int[3][]; //宣告二維陣列,並指定3列 x[0]=new int[3]; //指定第0列有3個元素 x[1]=new int[5]; //指定第1列有5個元素 x[2]=new int[2]; //指定第2列有2個元素

  22. 取陣列長度(元素)的方法(method) • 取二維陣列的列數、某列的元素個數 • 根據上一頁範例,可用以下程式碼得知陣列長度: x.length; //取得x陣列之長度為3列 x[1].length; //取得x陣列的第1列裡有5個元素 取得二維陣列的列數與特定列之元素的個數 陣列名稱.length // 取得陣列的列數 陣列名稱[列的索引值].length // 取得特定列元素的個數

  23. 範例-某汽車公司的車輛銷售表 public class Sample{ public static void main(String args[]){ int sale[][]={{32,35,26,30},{34,30,33,31}}; int sum=0; for(inti=0;i<sale.length;i++) { System.out.print("業務員"+(i+1)+"業績:"); for(int j=0;j<sale[i].length;j++) { System.out.print(sale[i][j]+" "); sum+=sale[i][j]; } System.out.print("總計:"+sum); System.out.println(); } } }

  24. 多維陣列 • 三維陣列 int A[2][4][3]; 2×4×3的三維陣列可看成是由2個4×3的二維陣列所組成 也就是兩組4個橫列,3個直行的積木併在一起,組成一個立方體

More Related