Oop2017 6
Oop2017 6
1
Opening Problem
Read one hundred numbers, compute their
average, and find out how many numbers are
above the average.
2
Solution
AnalyzeNumbers Run
3
Introducing Arrays
Array is a data structure that represents a collection of the
same types of data.
double[] myList = new double[10];
myList reference
myList[0] 5.6
myList[1] 4.5
Array reference myList[2] 3.3
variable
myList[3] 13.2
myList[4] 4
Array element at
myList[5] 34.33 Element value
index 5
myList[6] 34
myList[7] 45.45
myList[8] 99.993
myList[9] 11123
4
Declaring Array Variables
datatype[] arrayRefVar;
Example:
double[] myList;
5
Creating Arrays
arrayRefVar = new datatype[arraySize];
Example:
myList = new double[10];
6
Declaring and Creating
in One Step
datatype[] arrayRefVar = new
datatype[arraySize];
double[] myList = new double[10];
7
The Length of an Array
Once an array is created, its size is fixed. It cannot be
changed. You can find its size using
arrayName.length
For example,
myList.length returns 10
8
Default Values
When an array is created, its elements are
assigned the default value of
9
Indexed Variables
The array elements are accessed through the index. The
array indices are 0-based, i.e., it starts from 0 to
arrayRefVar.length-1. In the example in Figure 6.1,
myList holds ten double values and the indices are
from 0 to 9.
arrayName[index];
10
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 myList[0] and myList[1] to myList[2].
11
Array Initializers
Declaring, creating, initializing in one step:
double[] myList = {1.9, 2.9, 3.4, 3.5};
12
Declaring, creating, initializing
Using the Shorthand Notation
double[] myList = {1.9, 2.9, 3.4, 3.5};
13
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[] myList;
values[i] = i + values[i-1]; 2 0
} 3 0
}
}
15
animation
Trace Program with Arrays
i becomes 1
values[i] = i + values[i-1]; 1 0
0
} 2
3 0
values[0] = values[1] + values[4]; 4 0
}
}
16
animation
Trace Program with Arrays
i (=1) is less than 5
} 2 0
4 0
}
}
17
animation
Trace Program with Arrays
After this line is executed, value[1] is 1
values[i] = i + values[i-1]; 2 0
} 3 0
}
}
18
animation
} 2 0
3 0
values[0] = values[1] + values[4]; 4 0
}
}
19
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]; After the first iteration
} 2 0
3 0
values[0] = values[1] + 4 0
values[4];
}
}
20
animation
Trace Program with Arrays
After this line is executed,
values[2] is 3 (2 + 1)
values[i] = i + values[i-1]; 2 3
} 3 0
}
}
21
animation
Trace Program with Arrays
After this, i becomes 3.
values[i] = i + values[i-1]; 2 3
} 3 0
}
}
22
animation
Trace Program with Arrays
i (=3) is still less than 5.
values[i] = i + values[i-1]; 2 3
} 3 0
}
}
23
animation
Trace Program with Arrays
After this line, values[3] becomes 6 (3 + 3)
values[i] = i + values[i-1]; 2 3
} 3 6
}
}
24
animation
Trace Program with Arrays
After this, i becomes 4
values[i] = i + values[i-1]; 2 3
} 3 6
}
}
25
animation
Trace Program with Arrays
i (=4) is still less than 5
values[i] = i + values[i-1]; 2 3
} 3 6
}
}
26
animation
Trace Program with Arrays
After this, values[4] becomes 10 (4 + 6)
values[i] = i + values[i-1]; 2 3
} 3 6
}
}
27
animation
Trace Program with Arrays
After i++, i becomes 5
4 10
28
animation
1
0
1
values[0] = values[1] + values[4]; 2 3
} 3 6
} 4 10
29
animation
Trace Program with Arrays
After this line, values[0] is 11 (1 + 10)
values[i] = i + values[i-1]; 1 1
} 2 3
} 4 10
30
Processing Arrays
See the examples in the text.
1. (Initializing arrays with input values)
2. (Initializing arrays with random values)
3. (Printing arrays)
4. (Summing all elements)
5. (Finding the largest element)
6. (Finding the smallest index of the largest element)
7. (Shifting elements)
31
Initializing arrays with input values
java.util.Scanner input = new java.util.Scanner(System.in);
System.out.print("Enter " + myList.length + " values: ");
for (int i = 0; i < myList.length; i++)
myList[i] = input.nextDouble();
32
Initializing arrays with random values
33
Printing arrays
34
Summing all elements
double total = 0;
for (int i = 0; i < myList.length; i++) {
total += myList[i];
}
35
Finding the largest element
36
Shifting Elements
double temp = myList[0]; // Retain the first element
37
Enhanced for Loop (for-each 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 myList:
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.
38
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:
list2 = list1;
Before the assignment After the assignment
list2 = list1; list2 = list1;
list1 list1
Contents Contents
of list1 of list1
list2 list2
Contents Contents
of list2 of list2
Garbage
39
Copying Arrays
Using a loop:
int[] sourceArray = {2, 3, 1, 5, 10};
int[] targetArray = new
int[sourceArray.length];
40
The arraycopy Utility
arraycopy(sourceArray, src_pos,
targetArray, tar_pos, length);
Example:
System.arraycopy(sourceArray, 0,
targetArray, 0, sourceArray.length);
41
Passing Arrays to Methods
public static void printArray(int[] array) {
for (int i = 0; i < array.length; i++) {
System.out.print(array[i] + " ");
}
}
Anonymous array
42
Anonymous Array
The statement
printArray(new int[]{3, 1, 2, 6, 4, 2});
43
Pass By Value
Java uses pass by value to pass arguments to a method. There
are important differences between passing a value of variables
of primitive data types and passing arrays.
Objective:Demonstrate differences of
passing primitive data type variables
and array variables.
TestPassArray Run
46
Returning an Array from a Method
public static int[] reverse(int[] list) {
int[] result = new int[list.length];
return result;
}
list
result
47
animation
return result;
}
list 1 2 3 4 5 6
result 0 0 0 0 0 0
48
animation
return result;
}
list 1 2 3 4 5 6
result 0 0 0 0 0 0
49
animation
return result;
}
list 1 2 3 4 5 6
result 0 0 0 0 0 0
50
animation
return result;
}
list 1 2 3 4 5 6
result 0 0 0 0 0 1
51
animation
return result;
}
list 1 2 3 4 5 6
result 0 0 0 0 0 1
52
animation
return result;
}
list 1 2 3 4 5 6
result 0 0 0 0 0 1
53
animation
return result;
}
list 1 2 3 4 5 6
result 0 0 0 0 2 1
54
animation
return result;
}
list 1 2 3 4 5 6
result 0 0 0 0 2 1
55
animation
return result;
}
list 1 2 3 4 5 6
result 0 0 0 0 2 1
56
animation
return result;
}
list 1 2 3 4 5 6
result 0 0 0 3 2 1
57
animation
return result;
}
list 1 2 3 4 5 6
result 0 0 0 3 2 1
58
animation
return result;
}
list 1 2 3 4 5 6
result 0 0 0 3 2 1
59
animation
return result;
}
list 1 2 3 4 5 6
result 0 0 4 3 2 1
60
animation
return result;
}
list 1 2 3 4 5 6
result 0 0 4 3 2 1
61
animation
return result;
}
list 1 2 3 4 5 6
result 0 0 4 3 2 1
62
animation
return result;
}
list 1 2 3 4 5 6
result 0 5 4 3 2 1
63
animation
return result;
}
list 1 2 3 4 5 6
result 0 5 4 3 2 1
64
animation
return result;
}
list 1 2 3 4 5 6
result 0 5 4 3 2 1
65
animation
return result;
}
list 1 2 3 4 5 6
result 6 5 4 3 2 1
66
animation
return result;
}
list 1 2 3 4 5 6
result 6 5 4 3 2 1
67
animation
return result;
}
list 1 2 3 4 5 6
result 6 5 4 3 2 1
68
animation
return result;
}
list 1 2 3 4 5 6
list2
result 6 5 4 3 2 1
69
Searching Arrays
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.
70
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.
71
animation
3 6 4 1 9 7 3 2 8
3 6 4 1 9 7 3 2 8
3 6 4 1 9 7 3 2 8
3 6 4 1 9 7 3 2 8
72
From Idea to Solution
/** The method for finding a key in the list */
public static int linearSearch(int[] list, int key) {
for (int i = 0; i < list.length; i++)
if (key == list[i])
return i;
return -1;
}
74
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.
75