Liang Introduction to Java Programming Ninth Edition c

  • Slides: 31
Download presentation
第七章 多維陣列 Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc.

第七章 多維陣列 Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1

宣告/建立二維陣列 // Declare array ref var data. Type[][] ref. Var; // Create array and

宣告/建立二維陣列 // Declare array ref var data. Type[][] ref. Var; // Create array and assign its reference to variable ref. Var = new data. Type[10]; // Combine declaration and creation in one statement data. Type[][] ref. Var = new data. Type[10]; // Alternative syntax data. Type ref. Var[][] = new data. Type[10]; Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 4

宣告二維陣列變數與建立二維陣列 int[][] matrix = new int[10]; or int matrix[][] = new int[10]; matrix[0][0] =

宣告二維陣列變數與建立二維陣列 int[][] matrix = new int[10]; or int matrix[][] = new int[10]; matrix[0][0] = 3; for (int i = 0; i < matrix. length; i++) for (int j = 0; j < matrix[i]. length; j++) matrix[i][j] = (int)(Math. random() * 1000); double[][] x; Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 5

二維陣列 – 圖示 matrix. length? 5 array. length? 4 matrix[0]. length? 5 array[0]. length?

二維陣列 – 圖示 matrix. length? 5 array. length? 4 matrix[0]. length? 5 array[0]. length? 3 Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 6

宣告、建立,及初始化於同一敘述 我們也可以使用陣列初始化器來宣告、建立,及初始化 二維陣列。舉個例子: int[][] array = { {1, 2, 3}, {4, 5, 6}, {7,

宣告、建立,及初始化於同一敘述 我們也可以使用陣列初始化器來宣告、建立,及初始化 二維陣列。舉個例子: int[][] array = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {10, 11, 12} }; 相同 int[][] array = new int[4][3]; array[0][0] = 1; array[0][1] = 2; array[0][2] = 3; array[1][0] = 4; array[1][1] = 5; array[1][2] = 6; array[2][0] = 7; array[2][1] = 8; array[2][2] = 9; array[3][0] = 10; array[3][1] = 11; array[3][2] = 12; Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 7

二維陣列的長度 int[][] x = new int[3][4]; Liang, Introduction to Java Programming, Ninth Edition, (c)

二維陣列的長度 int[][] x = new int[3][4]; Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 8

二維陣列的長度 int[][] array = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9},

二維陣列的長度 int[][] array = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {10, 11, 12} }; array. length array[0]. length array[1]. length array[2]. length array[3]. length array[4]. length Array. Index. Out. Of. Bounds. Exception Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 9

不規則陣列 二維陣列裡的各列本身即為一個陣列,因此, 列可帶有不同的長度,這類陣列被車座不規則 陣列( ragged array )。舉例來說: int[][] matrix = { {1, 2, 3,

不規則陣列 二維陣列裡的各列本身即為一個陣列,因此, 列可帶有不同的長度,這類陣列被車座不規則 陣列( ragged array )。舉例來說: int[][] matrix = { {1, 2, 3, 4, 5}, {3, 4, 5}, {5} }; matrix. length is 5 matrix[0]. length is 5 matrix[1]. length is 4 matrix[2]. length is 3 matrix[3]. length is 2 matrix[4]. length is 1 Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 10

不規則陣列 Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All

不規則陣列 Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 11

利用輸入值初始化陣列 java. util. Scanner input = new Scanner(System. in); System. out. println("Enter " +

利用輸入值初始化陣列 java. util. Scanner input = new Scanner(System. in); System. out. println("Enter " + matrix. length + " rows and " + matrix[0]. length + " columns: "); for (int row = 0; row < matrix. length; row++) { for (int column = 0; column < matrix[row]. length; column++) { matrix[row][column] = input. next. Int(); } } Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 13

使用隨機數值初始化陣列 for (int row = 0; row < matrix. length; row++) { for (int

使用隨機數值初始化陣列 for (int row = 0; row < matrix. length; row++) { for (int column = 0; column < matrix[row]. length; column++) { matrix[row][column] = (int)(Math. random() * 100); } } Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 14

印出陣列 for (int row = 0; row < matrix. length; row++) { for (int

印出陣列 for (int row = 0; row < matrix. length; row++) { for (int column = 0; column < matrix[row]. length; column++) { System. out. print(matrix[row][column] + " "); } System. out. println(); } Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 15

加總所有元素 int total = 0; for (int row = 0; row < matrix. length;

加總所有元素 int total = 0; for (int row = 0; row < matrix. length; row++) { for (int column = 0; column < matrix[row]. length; column++) { total += matrix[row][column]; } } Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 16

對行上的元素做加總 for (int column = 0; column < matrix[0]. length; column++) { int total

對行上的元素做加總 for (int column = 0; column < matrix[0]. length; column++) { int total = 0; for (int row = 0; row < matrix. length; row++) total += matrix[row][column]; System. out. println("Sum for column " + column + " is " + total); } Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 17

隨機洗牌 for (int i = 0; i < matrix. length; i++) { for (int

隨機洗牌 for (int i = 0; i < matrix. length; i++) { for (int j = 0; j < matrix[i]. length; j++) { int i 1 = (int)(Math. random() * matrix. length); int j 1 = (int)(Math. random() * matrix[i]. length); // Swap matrix[i][j] with matrix[i 1][j 1] int temp = matrix[i][j]; matrix[i][j] = matrix[i 1][j 1]; matrix[i 1][j 1] = temp; } } Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 18

傳遞二維陣列給方法 Pass. Two. Dimensional. Array Run Liang, Introduction to Java Programming, Ninth Edition, (c)

傳遞二維陣列給方法 Pass. Two. Dimensional. Array Run Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 19

範例程式 7. 2 Grade. Exam. java F 目標:要撰寫一支為 多選題測驗打 分數的程式。 Grade. Exam Liang, Introduction

範例程式 7. 2 Grade. Exam. java F 目標:要撰寫一支為 多選題測驗打 分數的程式。 Grade. Exam Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. Run 20

範例程式 7. 3 Find. Nearest. Points. java Find. Nearest. Points Liang, Introduction to Java

範例程式 7. 3 Find. Nearest. Points. java Find. Nearest. Points Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. Run 21

答案是…? Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All

答案是…? Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 22

每行皆包含數字 1至 9 Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education,

每行皆包含數字 1至 9 Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 23

每列皆包含數字 1至 9 5 3 4 6 7 8 9 1 2 6 7

每列皆包含數字 1至 9 5 3 4 6 7 8 9 1 2 6 7 2 1 9 5 3 4 8 1 9 8 3 4 2 5 6 7 8 5 9 7 6 1 4 2 3 4 2 6 8 5 3 7 9 1 7 1 3 9 2 4 8 5 6 9 6 1 5 3 7 2 8 4 2 8 7 4 1 9 6 3 5 3 4 5 2 8 6 1 7 9 Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 24

每個3 x 3的區域內皆包含數字 1到 5 3 4 6 7 8 9 1 2 6

每個3 x 3的區域內皆包含數字 1到 5 3 4 6 7 8 9 1 2 6 7 2 1 9 5 3 4 8 1 9 8 3 4 2 5 6 7 8 5 9 7 6 1 4 2 3 4 2 6 8 5 3 7 9 1 7 1 3 9 2 4 8 5 6 9 6 1 5 3 7 2 8 4 2 8 7 4 1 9 6 3 5 3 4 5 2 8 6 1 7 9 Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 25

檢查答案是否是正確的? Check. Sudoku. Solution Run Liang, Introduction to Java Programming, Ninth Edition, (c) 2013

檢查答案是否是正確的? Check. Sudoku. Solution Run Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 26

多維陣列 double[][][] scores = { {{7. 5, 20. 5}, {9. 0, 22. 5}, {15,

多維陣列 double[][][] scores = { {{7. 5, 20. 5}, {9. 0, 22. 5}, {15, 33. 5}, {13, 21. 5}, {15, 2. 5}}, {{4. 5, 21. 5}, {9. 0, 22. 5}, {15, 34. 5}, {12, 20. 5}, {14, 9. 5}}, {{6. 5, 30. 5}, {9. 4, 10. 5}, {11, 33. 5}, {11, 23. 5}, {10, 2. 5}}, {{6. 5, 23. 5}, {9. 4, 32. 5}, {13, 34. 5}, {11, 20. 5}, {16, 7. 5}}, {{8. 5, 26. 5}, {9. 4, 52. 5}, {13, 36. 5}, {13, 24. 5}, {16, 2. 5}}, {{9. 5, 20. 5}, {9. 4, 42. 5}, {13, 31. 5}, {12, 20. 5}, {16, 6. 5}}}; Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 28

範例程式 7. 6 Guess. Birthday. Using. Array. java Guess. Birthday. java為一支猜 生日的程式。此程式可透過將五組數字儲 存於三維陣列中做簡化,其藉由迴圈提示 使用者輸入答案,如範例程式

範例程式 7. 6 Guess. Birthday. Using. Array. java Guess. Birthday. java為一支猜 生日的程式。此程式可透過將五組數字儲 存於三維陣列中做簡化,其藉由迴圈提示 使用者輸入答案,如範例程式 7. 6所示。此 範例執行結果會跟範例程式 3. 3相同。 F 範例程式 3. 3 Guess. Birthday. Using. Array Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. Run 31