Chapter 6 Arrays 1 Introducing Arrays Array is

  • Slides: 21
Download presentation
Chapter 6 Arrays 1

Chapter 6 Arrays 1

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. 2

Declaring Array Variables • datatype[] array. Ref. Var; Example: double[] my. List; • datatype

Declaring Array Variables • datatype[] array. Ref. Var; Example: double[] my. List; • datatype array. Ref. Var[]; // This style is correct, but not preferred Example: double my. List[]; 3

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

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. 4

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

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]; 5

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

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 6

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

Default Values 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. 7

Indexed Variables The array elements are accessed through the index. The array indices are

Indexed Variables The array elements are accessed through the index. The array indices are 0 -based, i. e. , it starts from 0 to array. Ref. Var. length-1. In the example in Figure 5. 1, my. List holds ten double values and the indices are from 0 to 9. Each element in the array is represented using the following syntax, known as an indexed variable: array. Ref. Var[index]; 8

Using Indexed Variables After an array is created, an indexed variable can be used

Using Indexed Variables After an array is created, an indexed variable can be used in the same way as a regular variable. For example, the following code adds the value in my. List[0] and my. List[1] to my. List[2] = my. List[0] + my. List[1]; 9

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

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. 10

Declaring, creating, initializing Using the Shorthand Notation double[] my. List = {1. 9, 2.

Declaring, creating, initializing Using the Shorthand Notation double[] my. List = {1. 9, 2. 9, 3. 4, 3. 5}; This shorthand notation is equivalent to the following statements: double[] my. List = new double[4]; my. List[0] = 1. 9; my. List[1] = 2. 9; my. List[2] = 3. 4; my. List[3] = 3. 5; 11

CAUTION Using the shorthand notation, you have to declare, create, and initialize the array

CAUTION Using the shorthand notation, you have to declare, create, and initialize the array all in one statement. Splitting it would cause a syntax error. For example, the following is wrong: double[] my. List; my. List = {1. 9, 2. 9, 3. 4, 3. 5}; 12

JDK 1. 5 Feature Enhanced for Loop JDK 1. 5 introduced a new for

JDK 1. 5 Feature Enhanced for Loop JDK 1. 5 introduced a new for loop that enables you to traverse the complete array sequentially without using an index variable. For example, the following code displays all elements in the array my. List: for (double value: my. List) System. out. println(value); In general, the syntax is 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. 13

Parallel Arrays • Parallel Arrays are simply two arrays created so that each element

Parallel Arrays • Parallel Arrays are simply two arrays created so that each element matches each other—that is, index numbers on each array connect two sets of data. • For example, you might have an array for product names and a matching array for prices. 14

Parallel Arrays: Example • A product name array: • String[] prod. Name = {“Dog

Parallel Arrays: Example • A product name array: • String[] prod. Name = {“Dog collar”, “Dog lead”, “Dog bowl”}; • A price array: • Double[] price = {9. 99, 15. 00, 21. 00}; 15

Example 5. 1 Testing Arrays • Objective: The program receives 6 numbers from the

Example 5. 1 Testing Arrays • Objective: The program receives 6 numbers from the keyboard, finds the largest number and counts the occurrence of the largest number entered from the keyboard. Suppose you entered 3, 5, 2, 5, 5, and 5, the largest number is 5 and its occurrence count is 4. 16

Example 5. 2 Assigning Grades • Objective: read student scores (int), get the best

Example 5. 2 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; Assign. Grade – Grade is C if score is >= best– 30; Run – Grade is D if score is >= best– 40; – Grade is F otherwise. 17

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

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; 18

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

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]; 19

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

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); 20

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

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 21