1 Chapter 6 Arrays Liang Introduction to Java

  • Slides: 106
Download presentation
1 Chapter 6 Arrays Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson

1 Chapter 6 Arrays Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671

Introducing Arrays Array is a data structure that represents a collection of the same

Introducing Arrays Array is a data structure that represents a collection of the same types of data. Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671 2

3 Declaring Array Variables datatype[] array. Ref. Var; OR datatype array. Ref. Var[]; //as

3 Declaring Array Variables datatype[] array. Ref. Var; OR datatype array. Ref. Var[]; //as in C array indices are 0 -based Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671

4 Creating Arrays array. Ref. Var = new datatype[array. Size]; Example: my. List =

4 Creating Arrays array. Ref. Var = new datatype[array. Size]; Example: my. List = new double[10]; my. List[0] references the first element in the array. my. List[9] references the last element in the array. Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671

5 Declaring and Creating in One Step • datatype[] array. Ref. Var = new

5 Declaring and Creating in One Step • datatype[] array. Ref. Var = new datatype[array. Size]; double[] my. List = new double[10]; • datatype array. Ref. Var[] = new datatype[array. Size]; double my. List[] = new double[10]; Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671

6 The Length of an Array Once an array is created, its size is

6 The Length of an Array Once an array is created, its size is fixed. It cannot be changed. You can find its size using array. Ref. Var. length For example, my. List. length returns 10 Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671

Default Values 7 When an array is created, its elements are assigned the default

Default Values 7 When an array is created, its elements are assigned the default value of 0 for the numeric primitive data types, 'u 0000' for char types, and false for boolean types. Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671

8 Array Initializers • Declaring, creating, initializing in one step: double[] my. List =

8 Array Initializers • Declaring, creating, initializing in one step: double[] my. List = {1. 9, 2. 9, 3. 4, 3. 5}; This shorthand syntax must be in one statement. Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671

animation Trace Program with Arrays Declare array variable values, create an array, and assign

animation Trace Program with Arrays Declare array variable values, create an array, and assign its reference to values public class Test { public static void main(String[] args) { int[] values = new int[5]; for (int i = 1; i < 5; i++) { values[i] = values[i] + values[i-1]; } values[0] = values[1] + values[4]; } } Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671 9

animation Trace Program with Arrays i becomes 1 public class Test { public static

animation Trace Program with Arrays i becomes 1 public class Test { public static void main(String[] args) { int[] values = new int[5]; for (int i = 1; i < 5; i++) { values[i] = values[i] + values[i-1]; } values[0] = values[1] + values[4]; } } Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671 10

animation Trace Program with Arrays i (=1) is less than 5 public class Test

animation Trace Program with Arrays i (=1) is less than 5 public class Test { public static void main(String[] args) { int[] values = new int[5]; for (int i = 1; i < 5; i++) { values[i] = values[i] + values[i-1]; } values[0] = values[1] + values[4]; } } Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671 11

animation Trace Program with Arrays After this line is executed, value[1] is 1 public

animation Trace Program with Arrays After this line is executed, value[1] is 1 public class Test { public static void main(String[] args) { int[] values = new int[5]; for (int i = 1; i < 5; i++) { values[i] = i + values[i-1]; } values[0] = values[1] + values[4]; } } Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671 12

animation Trace Program with Arrays After i++, i becomes 2 public class Test {

animation Trace Program with Arrays After i++, i becomes 2 public class Test { public static void main(String[] args) { int[] values = new int[5]; for (int i = 1; i < 5; i++) { values[i] = values[i] + values[i-1]; } values[0] = values[1] + values[4]; } } Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671 13

animation Trace Program with Arrays i (= 2) is less than 5 public class

animation Trace Program with Arrays i (= 2) is less than 5 public class Test { public static void main(String[] args) { int[] values = new int[5]; for (int i = 1; i < 5; i++) { values[i] = values[i] + values[i-1]; } values[0] = values[1] + values[4]; } } Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671 14

animation Trace Program with Arrays After this line is executed, values[2] is 3 (2

animation Trace Program with Arrays After this line is executed, values[2] is 3 (2 + 1) public class Test { public static void main(String[] args) { int[] values = new int[5]; for (int i = 1; i < 5; i++) { values[i] = i + values[i-1]; } values[0] = values[1] + values[4]; } } Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671 15

animation Trace Program with Arrays After this, i becomes 3. public class Test {

animation Trace Program with Arrays After this, i becomes 3. public class Test { public static void main(String[] args) { int[] values = new int[5]; for (int i = 1; i < 5; i++) { values[i] = i + values[i-1]; } values[0] = values[1] + values[4]; } } Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671 16

animation Trace Program with Arrays i (=3) is still less than 5. public class

animation Trace Program with Arrays i (=3) is still less than 5. public class Test { public static void main(String[] args) { int[] values = new int[5]; for (int i = 1; i < 5; i++) { values[i] = i + values[i-1]; } values[0] = values[1] + values[4]; } } Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671 17

animation Trace Program with Arrays After this line, values[3] becomes 6 (3 + 3)

animation Trace Program with Arrays After this line, values[3] becomes 6 (3 + 3) public class Test { public static void main(String[] args) { int[] values = new int[5]; for (int i = 1; i < 5; i++) { values[i] = i + values[i-1]; } values[0] = values[1] + values[4]; } } Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671 18

animation Trace Program with Arrays After this, i becomes 4 public class Test {

animation Trace Program with Arrays After this, i becomes 4 public class Test { public static void main(String[] args) { int[] values = new int[5]; for (int i = 1; i < 5; i++) { values[i] = i + values[i-1]; } values[0] = values[1] + values[4]; } } Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671 19

animation Trace Program with Arrays i (=4) is still less than 5 public class

animation Trace Program with Arrays i (=4) is still less than 5 public class Test { public static void main(String[] args) { int[] values = new int[5]; for (int i = 1; i < 5; i++) { values[i] = i + values[i-1]; } values[0] = values[1] + values[4]; } } Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671 20

animation Trace Program with Arrays After this, values[4] becomes 10 (4 + 6) public

animation Trace Program with Arrays After this, values[4] becomes 10 (4 + 6) public class Test { public static void main(String[] args) { int[] values = new int[5]; for (int i = 1; i < 5; i++) { values[i] = i + values[i-1]; } values[0] = values[1] + values[4]; } } Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671 21

animation Trace Program with Arrays After i++, i becomes 5 public class Test {

animation Trace Program with Arrays After i++, i becomes 5 public class Test { public static void main(String[] args) { int[] values = new int[5]; for (int i = 1; i < 5; i++) { values[i] = i + values[i-1]; } values[0] = values[1] + values[4]; } } Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671 22

23 animation Trace Program with Arrays i ( =5) < 5 is false. Exit

23 animation Trace Program with Arrays i ( =5) < 5 is false. Exit the loop public class Test { public static void main(String[] args) { int[] values = new int[5]; for (int i = 1; i < 5; i++) { values[i] = i + values[i-1]; } values[0] = values[1] + values[4]; } } Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671

animation Trace Program with Arrays After this line, values[0] is 11 (1 + 10)

animation Trace Program with Arrays After this line, values[0] is 11 (1 + 10) public class Test { public static void main(String[] args) { int[] values = new int[5]; for (int i = 1; i < 5; i++) { values[i] = i + values[i-1]; } values[0] = values[1] + values[4]; } } Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671 24

Enhanced for Loop (for-each loop) New for loop to traverse complete array without an

Enhanced for Loop (for-each loop) New for loop to traverse complete array without an index variable. for (double value: my. List) System. out. println(value); In general for (element. Type value: array. Ref. Var) { // Process the value } You still have to use an index variable if you wish to traverse the array in a different order or change the elements in the array. Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671 25

26 Example: Analyzing Array Elements • Objective: The program receives 6 numbers from the

26 Example: Analyzing Array Elements • Objective: The program receives 6 numbers from the user, finds the largest number and counts the occurrence of the largest number entered. Suppose you entered 3, 5, 2, 5, 5, and 5, the largest number is 5 and its occurrence count is 4. Test. Array Run Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671

27 Problem: Assigning Grades • Objective: read student scores (int), get the best score,

27 Problem: Assigning Grades • Objective: read student scores (int), get the best score, and then assign grades based on the following scheme: ▫ ▫ ▫ Grade is A if score is >= best– 10; Grade is B if score is >= best– 20; Grade is C if score is >= best– 30; Grade is D if score is >= best– 40; Grade is F otherwise. Assign. Grade Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671 Run

28 Copying Arrays Often, in a program, you need to duplicate an array or

28 Copying Arrays Often, in a program, you need to duplicate an array or a part of an array. In such cases you could attempt to use the assignment statement (=), as follows: list 2 = list 1; Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671

29 Copying Arrays Using a loop: int[] source. Array = {2, 3, 1, 5,

29 Copying Arrays Using a loop: int[] source. Array = {2, 3, 1, 5, 10}; int[] target. Array = new int[source. Array. length]; for (int i = 0; i < source. Arrays. length; i++) target. Array[i] = source. Array[i]; Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671

30 The arraycopy Utility arraycopy(source. Array, src_pos, target. Array, tar_pos, length); Example: System. arraycopy(source.

30 The arraycopy Utility arraycopy(source. Array, src_pos, target. Array, tar_pos, length); Example: System. arraycopy(source. Array, 0, target. Array, 0, source. Array. length); Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671

31 Passing Arrays to Methods public static void print. Array(int[] array) { for (int

31 Passing Arrays to Methods public static void print. Array(int[] array) { for (int i = 0; i < array. length; i++) { System. out. print(array[i] + " "); } } Invoke the method int[] list = {3, 1, 2, 6, 4, 2}; print. Array(list); Invoke the method print. Array(new int[]{3, 1, 2, 6, 4, 2}); Anonymous array Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671

Anonymous Array The statement print. Array(new int[]{3, 1, 2, 6, 4, 2}); creates an

Anonymous Array The statement print. Array(new int[]{3, 1, 2, 6, 4, 2}); creates an array using the following syntax: new data. Type[]{literal 0, literal 1, . . . , literalk}; There is no explicit reference variable for the array. Such array is called an anonymous array. Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671 32

33 Parameters Ø Pass by value Øactual value is passed Øchanges are local Ø

33 Parameters Ø Pass by value Øactual value is passed Øchanges are local Ø primitive data types Ø array elements Ø Pass by reference Ø memory address is passed Ø changes affect original data Ø reference types Ø arrays Ø objects Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671

Simple Example public class Test { public static void main(String[] args) { int x

Simple Example public class Test { public static void main(String[] args) { int x = 1; // x represents an int value int[] y = new int[10]; // y represents an array of int values m(x, y); // Invoke m with arguments x and y System. out. println("x is " + x); System. out. println("y[0] is " + y[0]); } public static void m(int number, int[] numbers) { number = 1001; // Assign a new value to numbers[0] = 5555; // Assign a new value to numbers[0] } } Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671 34

35 Passing Arrays as Arguments • Objective: Demonstrate differences of passing primitive data type

35 Passing Arrays as Arguments • Objective: Demonstrate differences of passing primitive data type variables and array variables. Test. Pass. Array Run Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671

Returning an Array from a Method public static int[] reverse(int[] list) { int[] result

Returning an Array from a Method public static int[] reverse(int[] list) { int[] result = new int[list. length]; for (int i = 0, j = result. length - 1; i < list. length; i++, j--) { result[j] = list[i]; } list return result; } result int[] list 1 = new int[]{1, 2, 3, 4, 5, 6}; int[] list 2 = reverse(list 1); Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671 36

37 animation Trace the reverse Method int[] list 1 = new int[]{1, 2, 3,

37 animation Trace the reverse Method int[] list 1 = new int[]{1, 2, 3, 4, 5, 6}; int[] list 2 = reverse(list 1); Declare result and create array public static int[] reverse(int[] list) { int[] result = new int[list. length]; for (int i = 0, j = result. length - 1; i < list. length; i++, j--) { result[j] = list[i]; } return result; } list result 1 2 3 4 5 6 0 0 0 Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671

animation Trace the reverse Method, cont. int[] list 1 = new int[]{1, 2, 3,

animation Trace the reverse Method, cont. int[] list 1 = new int[]{1, 2, 3, 4, 5, 6}; int[] list 2 = reverse(list 1); i = 0 and j = 5 public static int[] reverse(int[] list) { int[] result = new int[list. length]; for (int i = 0, j = result. length - 1; i < list. length; i++, j--) { result[j] = list[i]; } return result; } list result 1 2 3 4 5 6 0 0 0 Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671 38

animation Trace the reverse Method, cont. int[] list 1 = new int[]{1, 2, 3,

animation Trace the reverse Method, cont. int[] list 1 = new int[]{1, 2, 3, 4, 5, 6}; int[] list 2 = reverse(list 1); i (= 0) is less than 6 public static int[] reverse(int[] list) { int[] result = new int[list. length]; for (int i = 0, j = result. length - 1; i < list. length; i++, j--) { result[j] = list[i]; } return result; } list result 1 2 3 4 5 6 0 0 0 Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671 39

animation Trace the reverse Method, cont. 40 int[] list 1 = new int[]{1, 2,

animation Trace the reverse Method, cont. 40 int[] list 1 = new int[]{1, 2, 3, 4, 5, 6}; int[] list 2 = reverse(list 1); i = 0 and j = 5 Assign list[0] to result[5] public static int[] reverse(int[] list) { int[] result = new int[list. length]; for (int i = 0, j = result. length - 1; i < list. length; i++, j--) { result[j] = list[i]; } return result; } list result 1 2 3 4 5 6 0 0 0 1 Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671

animation Trace the reverse Method, cont. 41 int[] list 1 = new int[]{1, 2,

animation Trace the reverse Method, cont. 41 int[] list 1 = new int[]{1, 2, 3, 4, 5, 6}; int[] list 2 = reverse(list 1); After this, i becomes 1 and j becomes 4 public static int[] reverse(int[] list) { int[] result = new int[list. length]; for (int i = 0, j = result. length - 1; i < list. length; i++, j--) { result[j] = list[i]; } return result; } list result 1 2 3 4 5 6 0 0 0 1 Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671

animation Trace the reverse Method, cont. int[] list 1 = new int[]{1, 2, 3,

animation Trace the reverse Method, cont. int[] list 1 = new int[]{1, 2, 3, 4, 5, 6}; int[] list 2 = reverse(list 1); i (=1) is less than 6 public static int[] reverse(int[] list) { int[] result = new int[list. length]; for (int i = 0, j = result. length - 1; i < list. length; i++, j--) { result[j] = list[i]; } return result; } list result 1 2 3 4 5 6 0 0 0 1 Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671 42

animation Trace the reverse Method, cont. 43 int[] list 1 = new int[]{1, 2,

animation Trace the reverse Method, cont. 43 int[] list 1 = new int[]{1, 2, 3, 4, 5, 6}; int[] list 2 = reverse(list 1); i = 1 and j = 4 Assign list[1] to result[4] public static int[] reverse(int[] list) { int[] result = new int[list. length]; for (int i = 0, j = result. length - 1; i < list. length; i++, j--) { result[j] = list[i]; } return result; } list result 1 2 3 4 5 6 0 0 2 1 Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671

animation Trace the reverse Method, cont. 44 int[] list 1 = new int[]{1, 2,

animation Trace the reverse Method, cont. 44 int[] list 1 = new int[]{1, 2, 3, 4, 5, 6}; int[] list 2 = reverse(list 1); After this, i becomes 2 and j becomes 3 public static int[] reverse(int[] list) { int[] result = new int[list. length]; for (int i = 0, j = result. length - 1; i < list. length; i++, j--) { result[j] = list[i]; } return result; } list result 1 2 3 4 5 6 0 0 2 1 Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671

animation Trace the reverse Method, cont. int[] list 1 = new int[]{1, 2, 3,

animation Trace the reverse Method, cont. int[] list 1 = new int[]{1, 2, 3, 4, 5, 6}; int[] list 2 = reverse(list 1); i (=2) is still less than 6 public static int[] reverse(int[] list) { int[] result = new int[list. length]; for (int i = 0, j = result. length - 1; i < list. length; i++, j--) { result[j] = list[i]; } return result; } list result 1 2 3 4 5 6 0 0 2 1 Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671 45

animation Trace the reverse Method, cont. int[] list 1 = new int[]{1, 2, 3,

animation Trace the reverse Method, cont. int[] list 1 = new int[]{1, 2, 3, 4, 5, 6}; int[] list 2 = reverse(list 1); i = 2 and j = 3 Assign list[i] to result[j] public static int[] reverse(int[] list) { int[] result = new int[list. length]; for (int i = 0, j = result. length - 1; i < list. length; i++, j--) { result[j] = list[i]; } return result; } list result 1 2 3 4 5 6 0 0 0 3 2 1 Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671 46

animation Trace the reverse Method, cont. 47 int[] list 1 = new int[]{1, 2,

animation Trace the reverse Method, cont. 47 int[] list 1 = new int[]{1, 2, 3, 4, 5, 6}; int[] list 2 = reverse(list 1); After this, i becomes 3 and j becomes 2 public static int[] reverse(int[] list) { int[] result = new int[list. length]; for (int i = 0, j = result. length - 1; i < list. length; i++, j--) { result[j] = list[i]; } return result; } list result 1 2 3 4 5 6 0 0 0 3 2 1 Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671

animation Trace the reverse Method, cont. int[] list 1 = new int[]{1, 2, 3,

animation Trace the reverse Method, cont. int[] list 1 = new int[]{1, 2, 3, 4, 5, 6}; int[] list 2 = reverse(list 1); i (=3) is still less than 6 public static int[] reverse(int[] list) { int[] result = new int[list. length]; for (int i = 0, j = result. length - 1; i < list. length; i++, j--) { result[j] = list[i]; } return result; } list result 1 2 3 4 5 6 0 0 0 3 2 1 Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671 48

animation Trace the reverse Method, cont. int[] list 1 = new int[]{1, 2, 3,

animation Trace the reverse Method, cont. int[] list 1 = new int[]{1, 2, 3, 4, 5, 6}; int[] list 2 = reverse(list 1); i = 3 and j = 2 Assign list[i] to result[j] public static int[] reverse(int[] list) { int[] result = new int[list. length]; for (int i = 0, j = result. length - 1; i < list. length; i++, j--) { result[j] = list[i]; } return result; } list result 1 2 3 4 5 6 0 0 4 3 2 1 Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671 49

animation Trace the reverse Method, cont. 50 int[] list 1 = new int[]{1, 2,

animation Trace the reverse Method, cont. 50 int[] list 1 = new int[]{1, 2, 3, 4, 5, 6}; int[] list 2 = reverse(list 1); After this, i becomes 4 and j becomes 1 public static int[] reverse(int[] list) { int[] result = new int[list. length]; for (int i = 0, j = result. length - 1; i < list. length; i++, j--) { result[j] = list[i]; } return result; } list result 1 2 3 4 5 6 0 0 4 3 2 1 Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671

animation Trace the reverse Method, cont. int[] list 1 = new int[]{1, 2, 3,

animation Trace the reverse Method, cont. int[] list 1 = new int[]{1, 2, 3, 4, 5, 6}; int[] list 2 = reverse(list 1); i (=4) is still less than 6 public static int[] reverse(int[] list) { int[] result = new int[list. length]; for (int i = 0, j = result. length - 1; i < list. length; i++, j--) { result[j] = list[i]; } return result; } list result 1 2 3 4 5 6 0 0 4 3 2 1 Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671 51

animation Trace the reverse Method, cont. int[] list 1 = new int[]{1, 2, 3,

animation Trace the reverse Method, cont. int[] list 1 = new int[]{1, 2, 3, 4, 5, 6}; int[] list 2 = reverse(list 1); i = 4 and j = 1 Assign list[i] to result[j] public static int[] reverse(int[] list) { int[] result = new int[list. length]; for (int i = 0, j = result. length - 1; i < list. length; i++, j--) { result[j] = list[i]; } return result; } list result 1 2 3 4 5 6 0 5 4 3 2 1 Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671 52

animation Trace the reverse Method, cont. 53 int[] list 1 = new int[]{1, 2,

animation Trace the reverse Method, cont. 53 int[] list 1 = new int[]{1, 2, 3, 4, 5, 6}; int[] list 2 = reverse(list 1); After this, i becomes 5 and j becomes 0 public static int[] reverse(int[] list) { int[] result = new int[list. length]; for (int i = 0, j = result. length - 1; i < list. length; i++, j--) { result[j] = list[i]; } return result; } list result 1 2 3 4 5 6 0 5 4 3 2 1 Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671

animation Trace the reverse Method, cont. int[] list 1 = new int[]{1, 2, 3,

animation Trace the reverse Method, cont. int[] list 1 = new int[]{1, 2, 3, 4, 5, 6}; int[] list 2 = reverse(list 1); i (=5) is still less than 6 public static int[] reverse(int[] list) { int[] result = new int[list. length]; for (int i = 0, j = result. length - 1; i < list. length; i++, j--) { result[j] = list[i]; } return result; } list result 1 2 3 4 5 6 0 5 4 3 2 1 Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671 54

animation Trace the reverse Method, cont. int[] list 1 = new int[]{1, 2, 3,

animation Trace the reverse Method, cont. int[] list 1 = new int[]{1, 2, 3, 4, 5, 6}; int[] list 2 = reverse(list 1); i = 5 and j = 0 Assign list[i] to result[j] public static int[] reverse(int[] list) { int[] result = new int[list. length]; for (int i = 0, j = result. length - 1; i < list. length; i++, j--) { result[j] = list[i]; } return result; } list result 1 2 3 4 5 6 6 5 4 3 2 1 Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671 55

animation Trace the reverse Method, cont. 56 int[] list 1 = new int[]{1, 2,

animation Trace the reverse Method, cont. 56 int[] list 1 = new int[]{1, 2, 3, 4, 5, 6}; int[] list 2 = reverse(list 1); After this, i becomes 6 and j becomes -1 public static int[] reverse(int[] list) { int[] result = new int[list. length]; for (int i = 0, j = result. length - 1; i < list. length; i++, j--) { result[j] = list[i]; } return result; } list result 1 2 3 4 5 6 6 5 4 3 2 1 Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671

animation Trace the reverse Method, cont. 57 int[] list 1 = new int[]{1, 2,

animation Trace the reverse Method, cont. 57 int[] list 1 = new int[]{1, 2, 3, 4, 5, 6}; int[] list 2 = reverse(list 1); i (=6) < 6 is false. So exit the loop. public static int[] reverse(int[] list) { int[] result = new int[list. length]; for (int i = 0, j = result. length - 1; i < list. length; i++, j--) { result[j] = list[i]; } return result; } list result 1 2 3 4 5 6 6 5 4 3 2 1 Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671

animation Trace the reverse Method, cont. int[] list 1 = new int[]{1, 2, 3,

animation Trace the reverse Method, cont. int[] list 1 = new int[]{1, 2, 3, 4, 5, 6}; int[] list 2 = reverse(list 1); Return result public static int[] reverse(int[] list) { int[] result = new int[list. length]; for (int i = 0, j = result. length - 1; i < list. length; i++, j--) { result[j] = list[i]; } return result; } list 1 2 3 4 5 6 6 5 4 3 2 1 list 2 result Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671 58

59 Problem: Counting Occurrence of Each Letter • Generate 100 lowercase letters randomly and

59 Problem: Counting Occurrence of Each Letter • Generate 100 lowercase letters randomly and assign to an array of characters. • Count the occurrence of each letter in the array. Count. Letters. In. Array Run Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671

Searching Arrays 60 Searching is the process of looking for a specific element in

Searching Arrays 60 Searching is the process of looking for a specific element in an array; for example, discovering whether a certain score is included in a list of scores. Searching is a common task in computer programming. There are many algorithms and data structures devoted to searching. In this section, two commonly used approaches are discussed, linear search and binary search. Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671

61 Linear Search The linear search approach compares the key element, key, sequentially with

61 Linear Search The linear search approach compares the key element, key, sequentially with each element in the array list. The method continues to do so until the key matches an element in the list or the list is exhausted without a match being found. If a match is made, the linear search returns the index of the element in the array that matches the key. If no match is found, the search returns 1. Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671

62 animation Linear Search Animation Key List 3 6 4 1 9 7 3

62 animation Linear Search Animation Key List 3 6 4 1 9 7 3 2 8 3 6 4 1 9 7 3 2 8 Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671

63 From Idea to Solution /** The method for finding a key in the

63 From Idea to Solution /** The method for finding a key in the list */ public static int linear. Search(int[] list, int key) { for (int i = 0; i < list. length; i++) if (key == list[i]) return i; return -1; } Trace the method int[] list = {1, 4, 4, 2, 5, -3, 6, 2}; int i = linear. Search(list, 4); // returns 1 int j = linear. Search(list, -4); // returns -1 int k = linear. Search(list, -3); // returns 5 Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671

64 Linear Search Applet An applet was developed by a student to visualize the

64 Linear Search Applet An applet was developed by a student to visualize the steps for linear search Linear Search source code Linear Search Applet Linear. Search Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671

65 Binary Search For binary search to work, the elements in the array must

65 Binary Search For binary search to work, the elements in the array must already be ordered. Without loss of generality, assume that the array is in ascending order. e. g. , 2 4 7 10 11 45 50 59 60 66 69 70 79 The binary search first compares the key with the element in the middle of the array. Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671

66 Binary Search, cont. Consider the following three cases: • • • If the

66 Binary Search, cont. Consider the following three cases: • • • If the key is less than the middle element, you only need to search the key in the first half of the array. If the key is equal to the middle element, the search ends with a match. If the key is greater than the middle element, you only need to search the key in the second half of the array. Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671

67 animation Binary Search Key List 8 1 2 3 4 6 7 8

67 animation Binary Search Key List 8 1 2 3 4 6 7 8 9 Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671

Binary Search, cont. Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education,

Binary Search, cont. Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671 68

Binary Search, cont. Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education,

Binary Search, cont. Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671 69

Binary Search, cont. 70 The binary. Search method returns the index of the element

Binary Search, cont. 70 The binary. Search method returns the index of the element in the list that matches the search key if it is contained in the list. Otherwise, it returns -insertion point - 1. The insertion point is the point at which the key would be inserted into the list. Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671

From Idea to Soluton /** Use binary search to find the key in the

From Idea to Soluton /** Use binary search to find the key in the list */ public static int binary. Search(int[] list, int key) { int low = 0; int high = list. length - 1; while (high >= low) { int mid = (low + high) / 2; if (key < list[mid]) high = mid - 1; else if (key == list[mid]) return mid; else low = mid + 1; } return -1 - low; } Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671 71

72 Binary Search Applet An applet was developed by a student to visualize the

72 Binary Search Applet An applet was developed by a student to visualize the steps for binary search Binary Search source code Binary Search Applet Binary. Search Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671

73 The Arrays. binary. Search Method Overloaded binary. Search methods for searching a key

73 The Arrays. binary. Search Method Overloaded binary. Search methods for searching a key in an array of int, double, char, short, long, and float (java. util. Arrays class) int[] list = {2, 4, 7, 10, 11, 45, 50, 59, 60, 66, 69, 70, 79}; Return is 4 System. out. println("Index is " + java. util. Arrays. binary. Search(list, 11)); Return is – 4 (insertion point is char[] chars = {'a', 'c', 'g', 'x', 'y', 'z'}; 3, so return is -3 -1) System. out. println("Index is " + java. util. Arrays. binary. Search(chars, 't')); Parameter array must be pre-sorted in increasing order. Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671

Sorting Arrays 74 Sorting, like searching, is also a common task in computer programming.

Sorting Arrays 74 Sorting, like searching, is also a common task in computer programming. It would be used, for instance, if you wanted to display the grades from Listing 6. 2, “Assigning Grades, ” in alphabetical order. Many different algorithms have been developed for sorting. This section introduces two simple, intuitive sorting algorithms: selection sort and insertion sort. Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671

Selection Sort Selection sort finds the largest number in the list and places it

Selection Sort Selection sort finds the largest number in the list and places it last. It then finds the largest number remaining and places it next to last, and so on until the list contains only a single number. Figure 6. 17 shows how to sort the list {2, 9, 5, 4, 8, 1, 6} using selection sort. Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671 75

76 animation Selection Sort int[] my. List = {2, 9, 5, 4, 8, 1,

76 animation Selection Sort int[] my. List = {2, 9, 5, 4, 8, 1, 6}; // Unsorted 2 2 2 1 9 6 1 2 5 5 4 4 5 5 8 1 6 6 1 8 8 8 6 2 6 5 4 8 1 9 2 1 5 4 6 8 9 2 1 4 5 6 8 9 9 Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671

77 From Idea to Solution for (int i = list. length - 1; i

77 From Idea to Solution for (int i = list. length - 1; i >= 1; i--) { select the largest element in list[0. . i]; swap the largest with list[i], if necessary; // list[i] is in place. The next iteration applies on list[0. . i-1] } list[0] list[1] list[2] list[3]. . . list[10] list[1] list[2] list[3]. . . list[9] list[0] list[1] list[2] list[3]. . . list[8] list[0] list[1] list[2] list[3]. . . list[7]. . . list[0] Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671

78 for (int i = list. length - 1; i >= 1; i--) {

78 for (int i = list. length - 1; i >= 1; i--) { select the largest element in list[0. . i]; swap the largest with list[i], if necessary; // list[i] is in place. The next iteration applies on list[0. . i-1] } Expand // Find the maximum in the list[0. . i] double current. Max = list[0]; int current. Max. Index = 0; for (int j = 1; j <= i; j++) { if (current. Max < list[j]) { current. Max = list[j]; current. Max. Index = j; } } Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671

79 for (int i = list. length - 1; i >= 1; i--) {

79 for (int i = list. length - 1; i >= 1; i--) { select the largest element in list[0. . i]; swap the largest with list[i], if necessary; // list[i] is in place. The next iteration applies on list[0. . i-1] } Expand // Find the maximum in the list[0. . i] double current. Max = list[0]; int current. Max. Index = 0; for (int j = 1; j <= i; j++) { if (current. Max < list[j]) { current. Max = list[j]; current. Max. Index = j; } } Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671

80 for (int i = list. length - 1; i >= 1; i--) {

80 for (int i = list. length - 1; i >= 1; i--) { select the largest element in list[0. . i]; swap the largest with list[i], if necessary; // list[i] is in place. The next iteration applies on list[0. . i-1] } Expand // Swap list[i] with list[current. Max. Index] if necessary; if (current. Max. Index != i) { list[current. Max. Index] = list[i]; list[i] = current. Max; } Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671

Wrap it in a Method 81 /** The method for sorting the numbers */

Wrap it in a Method 81 /** The method for sorting the numbers */ public static void selection. Sort(double[] list) { for (int i = list. length - 1; i >= 1; i--) { // Find the maximum in the list[0. . i] double current. Max = list[0]; int current. Max. Index = 0; Invoke it for (int j = 1; j <= i; j++) { selection. Sort(your. List) if (current. Max < list[j]) { current. Max = list[j]; current. Max. Index = j; } } // Swap list[i] with list[current. Max. Index] if necessary; if (current. Max. Index != i) { list[current. Max. Index] = list[i]; list[i] = current. Max; } } } Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671

82 Selection Sort Applet An applet was developed by a student to visualize the

82 Selection Sort Applet An applet was developed by a student to visualize the steps for selection sort Selection Sort source code Selection Sort Applet Selection. Sort Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671

83 Insertion Sort int[] my. List = {2, 9, 5, 4, 8, 1, 6};

83 Insertion Sort int[] my. List = {2, 9, 5, 4, 8, 1, 6}; // Unsorted The insertion sort algorithm sorts a list of values by repeatedly inserting an unsorted element into a sorted sublist until the whole list is sorted. Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671

84 animation int[] my. List = {2, 9, 5, 4, 8, 1, 6}; //

84 animation int[] my. List = {2, 9, 5, 4, 8, 1, 6}; // Unsorted Insertion Sort 2 9 5 4 8 1 6 2 5 9 4 8 1 6 2 4 1 2 5 4 8 5 9 1 6 8 2 9 5 4 8 1 6 2 4 5 9 8 1 6 1 2 4 5 8 9 6 6 9 Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671

85 How to Insert? The insertion sort algorithm sorts a list of values by

85 How to Insert? The insertion sort algorithm sorts a list of values by repeatedly inserting an unsorted element into a sorted sublist until the whole list is sorted. Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671

86 From Idea to Solution for (int i = 1; 1; i < list,

86 From Idea to Solution for (int i = 1; 1; i < list, length; i++) { insert list[i] into a sorted sublist[0. . i-1] so that list[0. . i] is sorted } list[0] list[1] list[2] list[3] list[0] list[1] list[2] list[3]. . . Insert. Sort Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671

87 The Arrays. sort Method Since sorting is frequently used in programming, Java provides

87 The Arrays. sort Method Since sorting is frequently used in programming, Java provides several overloaded sort methods for sorting an array of int, double, char, short, long, and float in the java. util. Arrays class. For example, the following code sorts an array of numbers and an array of characters. double[] numbers = {6. 0, 4. 4, 1. 9, 2. 9, 3. 4, 3. 5}; java. util. Arrays. sort(numbers); char[] chars = {'a', 'A', '4', 'F', 'D', 'P'}; java. util. Arrays. sort(chars); Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671

Two-dimensional Arrays 2 -dimensional array can represent matrix or table. Distances between cities: Liang,

Two-dimensional Arrays 2 -dimensional array can represent matrix or table. Distances between cities: Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671 88

89 Declare/Create Two-dimensional Arrays // Declare array ref var data. Type[][] ref. Var; //

89 Declare/Create Two-dimensional Arrays // 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]; //data. Type [row. Index] [column. Index] Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671

Declaring Variables of Twodimensional Arrays and Creating Twodimensional Arrays 90 int[][] matrix = new

Declaring Variables of Twodimensional Arrays and Creating Twodimensional Arrays 90 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, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671

Two-dimensional Array Illustration matrix. length? 5 array. length? 4 matrix[0]. length? 5 array[0]. length?

Two-dimensional Array Illustration matrix. length? 5 array. length? 4 matrix[0]. length? 5 array[0]. length? 3 Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671 91

Declaring, Creating, and Initializing Using Shorthand Notations 92 You can also use an array

Declaring, Creating, and Initializing Using Shorthand Notations 92 You can also use an array initializer to declare, create and initialize a two-dimensional array. For example, int[][] array = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {10, 11, 12} }; Same as 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, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671

93 Lengths of Two-dimensional Arrays int[][] x = new int[3][4]; Liang, Introduction to Java

93 Lengths of Two-dimensional Arrays int[][] x = new int[3][4]; Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671

Lengths of Two-dimensional Arrays, cont. int[][] array = { {1, 2, 3}, {4, 5,

Lengths of Two-dimensional Arrays, cont. int[][] array = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {10, 11, 12} }; 94 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, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671

95 Ragged Arrays Each row in a two-dimensional array is itself an array. So,

95 Ragged Arrays Each row in a two-dimensional array is itself an array. So, the rows can have different lengths. Such an array is known as a ragged array. For example, int[][] matrix = { {1, 2, 3, 4, 5}, matrix. length is 5 {2, 3, 4, 5}, matrix[0]. length is 5 matrix[1]. length is 4 {3, 4, 5}, matrix[2]. length is 3 {4, 5}, matrix[3]. length is 2 {5} matrix[4]. length is 1 }; Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671

96 Ragged Arrays, cont. Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson

96 Ragged Arrays, cont. Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671

97 Problem: Grading Multiple. Choice Test • Objective: write a program that grades multiple-choice

97 Problem: Grading Multiple. Choice Test • Objective: write a program that grades multiple-choice test. Grade. Exam Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671 Run

Problem: Finding Two Points Nearest to Each Other Find. Nearest. Points Liang, Introduction to

Problem: Finding Two Points Nearest to Each Other Find. Nearest. Points Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671 Run 98

99 Case Study: Sudoku The objective is to fill the grid (see Figure 6.

99 Case Study: Sudoku The objective is to fill the grid (see Figure 6. 14(a)) so that every row, every column, and every 3× 3 box contain the numbers 1 to 9, as shown in Figure 6. 14(b). Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671

100 Case Study: Sudoku Run with prepared input Liang, Introduction to Java Programming, Seventh

100 Case Study: Sudoku Run with prepared input Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671

Multidimensional Arrays n-dimensional arrays for any integer n Generalization of declaration and creation of

Multidimensional Arrays n-dimensional arrays for any integer n Generalization of declaration and creation of 2 -D arrays to n dimensions double[][][] scores = new double[10][5][2]; will Ø declare a three-dimensional array variable Ø create array Ø assign reference Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671 101

102 Problem: Calculating Total Scores • Objective: write a program that calculates the total

102 Problem: Calculating Total Scores • Objective: write a program that calculates the total score for students in a class. Suppose the scores are stored in a threedimensional array named scores. The first index in scores refers to a student, the second refers to an exam, and the third refers to the part of the exam. Suppose there are 7 students, 5 exams, and each exam has two parts--the multiple-choice part and the programming part. So, scores[i][j][0] represents the score on the multiple-choice part for the i’s student on the j’s exam. Your program displays the total score for each student. Total. Score Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671 Run

103 Main Method Is Just a Regular Method Call a regular method by passing

103 Main Method Is Just a Regular Method Call a regular method by passing actual parameters. Can you pass arguments to main? YES! Can you call main? YES! Main can be used like any other method. Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671

104 Command-Line Parameters class Test. Main { public static void main(String[] args) { .

104 Command-Line Parameters class Test. Main { public static void main(String[] args) { . . . } } java Test. Main arg 0 arg 1 arg 2. . . argn Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671

105 Processing Command-Line Parameters In the main method, get the arguments from args[0], args[1],

105 Processing Command-Line Parameters In the main method, get the arguments from args[0], args[1], . . . , args[n], which corresponds to arg 0, arg 1, . . . , argn in the command line. Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671

106 Input - Scanner Object Read. Data Run Liang, Introduction to Java Programming, Seventh

106 Input - Scanner Object Read. Data Run Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671