Original Slides by Daniel Liang Modified Slides by Salam Abdulla
Original Slides by Daniel Liang Modified Slides by Salam Abdulla
Single-Dimensional Arrays
Computer Programming Fundementals
College of Science and Technology- IT & CS
2
Objectives
4
Declaring Array Variables
• datatype[] arrayRefVar;
Example:
double[] myList;
5
Creating Arrays
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
arrayRefVar.length
For example,
myList.length returns 10
8
Default Values
9
Indexed Variables
arrayRefVar[index]; 10
Using Indexed Variables
11
Array Initializers
12
Declaring, creating, initializing Using the
Shorthand Notation
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;
}
}
15
animation
Trace Program with Arrays
i becomes 1
values[i] = i + values[i-1]; 1 0
} 2 0
3 0
values[0] = values[1] + values[4];
4 0
}
}
16
animation
Trace Program with Arrays
i (=1) is less than 5
} 4 0
17
animation
Trace Program with Arrays
After this line is executed, value[1] is 1
} 3 0
}
}
18
animation
Trace Program with Arrays
After i++, i becomes 2
} 2 0
0
values[0] = values[1] + values[4]; 3
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
values[0] = values[1] + 3 0
values[4]; 4 0
}
}
20
animation
Trace Program with Arrays
After this line is executed,
values[2] is 3 (2 + 1)
} 3 0
}
}
21
animation
Trace Program with Arrays
After this, i becomes 3.
} 3 0
}
}
22
animation
Trace Program with Arrays
} 3 0
}
}
23
animation
Trace Program with Arrays
} 3 6
}
}
24
animation
Trace Program with Arrays
After this, i becomes 4
} 3 6
}
}
25
animation
Trace Program with Arrays
i (=4) is still less than 5
} 3 6
}
}
26
animation
Trace Program with Arrays
After this, values[4] becomes 10 (4 + 6)
} 3 6
}
}
27
animation
Trace Program with Arrays
3 6
4 10
28
animation
1 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
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
Random shuffling
for (int i = 0; i < myList.length - 1; i++) { myList
// Generate an index j randomly i [0] .
int j = (int)(Math.random() [1] .
* myList.length); .
[i] .
// Swap myList[i] with myList[j] .
double temp = myList[i]; . swap
myList[i] = myList[j]; A random index [j]
myList[j] = temp;
}
37
Shifting Elements
38
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:
for (double value: myList)
System.out.println(value);
In general, the syntax is
for (elementType value: arrayRefVar) {
// 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.
39
Analyze Numbers
AnalyzeNumbers Run
40
Problem: Deck of Cards
41
Problem: Deck of Cards, cont.
42
Problem: Deck of Cards, cont.
43
Problem: Deck of Cards
45
Companion
Website
Problem: Lotto Numbers
[2] false [2] false [2] false [2] true [2] true
[3] false [3] false [3] false [3] false [3] false
. . . . .
. . . . .
. . . . .
46
Copying Arrays
47
Copying Arrays
Using a loop:
int[] sourceArray = {2, 3, 1, 5, 10};
int[] targetArray = new
int[sourceArray.length];
48
The arraycopy Utility
arraycopy(sourceArray, src_pos,
targetArray, tar_pos, length);
Example:
System.arraycopy(sourceArray, 0,
targetArray, 0, sourceArray.length);
49
Passing Arrays to Methods
public static void printArray(int[] array) {
for (int i = 0; i < array.length; i++) {
System.out.print(array[i] + " ");
}
}
Invoke the method
Anonymous array
50
Anonymous Array
The statement
printArray(new int[]{3, 1, 2, 6, 4, 2});
creates an array using the following syntax:
new dataType[]{literal0, literal1, ..., literalk};
There is no explicit reference variable for the array.
Such array is called an anonymous array.
51
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.
Heap
TestPassArray Run
57
Example, cont.
Stack Heap Stack
Space required for the
Space required for the swapFirstTwoInArray
swap method method
n2: 2 int[] array reference
n1: 1
58
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
59
animation
Trace the reverse Method
int[] list1 = {1, 2, 3, 4, 5, 6};
int[] list2 = reverse(list1);
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 1 2 3 4 5 6
result 0 0 0 0 0 0
60
animation
Trace the reverse Method, cont.
int[] list1 = {1, 2, 3, 4, 5, 6};
int[] list2 = reverse(list1);
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 1 2 3 4 5 6
result 0 0 0 0 0 0
61
animation
Trace the reverse Method, cont.
int[] list1 = {1, 2, 3, 4, 5, 6};
int[] list2 = reverse(list1);
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 1 2 3 4 5 6
result 0 0 0 0 0 0
62
animation
Trace the reverse Method, cont.
int[] list1 = {1, 2, 3, 4, 5, 6};
int[] list2 = reverse(list1);
i = 0 and j = 5
public static int[] reverse(int[] list) { Assign list[0] to result[5]
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
result 0 0 0 0 0 1
63
animation
Trace the reverse Method, cont.
int[] list1 = {1, 2, 3, 4, 5, 6};
int[] list2 = reverse(list1);
list 1 2 3 4 5 6
result 0 0 0 0 0 1
64
animation
Trace the reverse Method, cont.
int[] list1 = {1, 2, 3, 4, 5, 6};
int[] list2 = reverse(list1);
list 1 2 3 4 5 6
result 0 0 0 0 0 1
65
animation
Trace the reverse Method, cont.
int[] list1 = {1, 2, 3, 4, 5, 6};
int[] list2 = reverse(list1);
i = 1 and j = 4
public static int[] reverse(int[] list) { Assign list[1] to result[4]
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
result 0 0 0 0 2 1
66
animation
Trace the reverse Method, cont.
int[] list1 = {1, 2, 3, 4, 5, 6};
int[] list2 = reverse(list1);
After this, i becomes 2 and
public static int[] reverse(int[] list) { j becomes 3
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
result 0 0 0 0 2 1
67
animation
Trace the reverse Method, cont.
int[] list1 = {1, 2, 3, 4, 5, 6};
int[] list2 = reverse(list1);
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 1 2 3 4 5 6
result 0 0 0 0 2 1
68
animation
Trace the reverse Method, cont.
int[] list1 = {1, 2, 3, 4, 5, 6};
int[] list2 = reverse(list1);
i = 2 and j = 3
public static int[] reverse(int[] list) { Assign list[i] to result[j]
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
result 0 0 0 3 2 1
69
animation
Trace the reverse Method, cont.
int[] list1 = {1, 2, 3, 4, 5, 6};
int[] list2 = reverse(list1);
After this, i becomes 3 and
public static int[] reverse(int[] list) { j becomes 2
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
result 0 0 0 3 2 1
70
animation
Trace the reverse Method, cont.
int[] list1 = {1, 2, 3, 4, 5, 6};
int[] list2 = reverse(list1);
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 1 2 3 4 5 6
result 0 0 0 3 2 1
71
animation
Trace the reverse Method, cont.
int[] list1 = {1, 2, 3, 4, 5, 6};
int[] list2 = reverse(list1);
i = 3 and j = 2
public static int[] reverse(int[] list) { Assign list[i] to result[j]
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
result 0 0 4 3 2 1
72
animation
Trace the reverse Method, cont.
int[] list1 = {1, 2, 3, 4, 5, 6};
int[] list2 = reverse(list1);
After this, i becomes 4 and
public static int[] reverse(int[] list) { j becomes 1
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
result 0 0 4 3 2 1
73
animation
Trace the reverse Method, cont.
int[] list1 = {1, 2, 3, 4, 5, 6};
int[] list2 = reverse(list1);
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 1 2 3 4 5 6
result 0 0 4 3 2 1
74
animation
Trace the reverse Method, cont.
int[] list1 = {1, 2, 3, 4, 5, 6};
int[] list2 = reverse(list1);
i = 4 and j = 1
public static int[] reverse(int[] list) { Assign list[i] to result[j]
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
result 0 5 4 3 2 1
75
animation
Trace the reverse Method, cont.
int[] list1 = {1, 2, 3, 4, 5, 6};
int[] list2 = reverse(list1);
After this, i becomes 5 and
public static int[] reverse(int[] list) { j becomes 0
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
result 0 5 4 3 2 1
76
animation
Trace the reverse Method, cont.
int[] list1 = {1, 2, 3, 4, 5, 6};
int[] list2 = reverse(list1);
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 1 2 3 4 5 6
result 0 5 4 3 2 1
77
animation
Trace the reverse Method, cont.
int[] list1 = {1, 2, 3, 4, 5, 6};
int[] list2 = reverse(list1);
i = 5 and j = 0
public static int[] reverse(int[] list) { Assign list[i] to result[j]
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
result 6 5 4 3 2 1
78
animation
Trace the reverse Method, cont.
int[] list1 = {1, 2, 3, 4, 5, 6};
int[] list2 = reverse(list1);
After this, i becomes 6 and
public static int[] reverse(int[] list) { j becomes -1
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
result 6 5 4 3 2 1
79
animation
Trace the reverse Method, cont.
int[] list1 = {1, 2, 3, 4, 5, 6};
int[] list2 = reverse(list1);
i (=6) < 6 is false. So exit
public static int[] reverse(int[] list) { the loop.
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
result 6 5 4 3 2 1
80
animation
Trace the reverse Method, cont.
int[] list1 = {1, 2, 3, 4, 5, 6};
int[] list2 = reverse(list1);
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
list2
result 6 5 4 3 2 1
81
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.
CountLettersInArray Run
82
Variable-Length Arguments
VarArgsDemo Run
83
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. In this section, two
commonly used approaches are discussed, linear search
and binary search.
public class LinearSearch {
/** 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]) [0] [1] [2] …
return i; list
return -1;
} key Compare key with list[i] for i = 0, 1, …
}
84
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.
85
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
3 6 4 1 9 7 3 2 8
86
animation
Linear Search Animation
http://www.cs.armstrong.edu/liang/animation/web/Line
arSearch.html
87
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;
}
89
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.
90
animation
Binary Search
Key List
8 1 2 3 4 6 7 8 9
8 1 2 3 4 6 7 8 9
8 1 2 3 4 6 7 8 9
91
animation
Binary Search Animation
http://www.cs.armstrong.edu/liang/animation/web/Bina
rySearch.html
92
Binary Search, cont.
93
Binary Search, cont.
key is 54 low mid high
key > 50 [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12]
list 2 4 7 10 11 45 50 59 60 66 69 70 79
low mid high
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12]
key < 66 list 59 60 66 69 70 79
[7] [8]
key < 59 list 59 60
low high
-insertion point - 1.
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.binarySearch(list, 11));
char[] chars = {'a', 'c', 'g', 'x', 'y', 'z'}; Return is –4 (insertion point
System.out.println("Index is " + is 3, so return is -3-1)
java.util.Arrays.binarySearch(chars, 't'));
For the binarySearch method to work, the array must be pre-sorted in increasing
order.
97
Sorting Arrays
98
Selection Sort
Selection sort finds the smallest number in the list and places it first. It then finds
the smallest number remaining and places it second, and so on until the list
contains only a single number.
99
animation
Selection Sort Animation
http://www.cs.armstrong.edu/liang/animation/web/Sele
ctionSort.html
100
From Idea to Solution
...
Expand
double currentMin = list[i];
int currentMinIndex = i;
for (int j = i+1; j < list.length; j++) {
if (currentMin > list[j]) {
currentMin = list[j];
currentMinIndex = j;
}
}
102
for (int i = 0; i < listSize; i++) {
select the smallest element in list[i..listSize-1];
swap the smallest with list[i], if necessary;
// list[i] is in its correct position.
// The next iteration apply on list[i..listSize-1]
}
Expand
double currentMin = list[i];
int currentMinIndex = i;
for (int j = i; j < list.length; j++) {
if (currentMin > list[j]) {
currentMin = list[j];
currentMinIndex = j;
}
}
103
for (int i = 0; i < listSize; i++) {
select the smallest element in list[i..listSize-1];
swap the smallest with list[i], if necessary;
// list[i] is in its correct position.
// The next iteration apply on list[i..listSize-1]
}
Expand
if (currentMinIndex != i) {
list[currentMinIndex] = list[i];
list[i] = currentMin;
}
104
Wrap it in a Method
Invoke
// Swap list[i] with list[currentMinIndex] it
if necessary;
if (currentMinIndex != i) {
list[currentMinIndex] = list[i]; selectionSort(yourList)
list[i] = currentMin;
}
}
}
105
The Arrays.sort Method
106
The Arrays.toString(list) Method
107
Pass Arguments to Invoke the Main
Method
108
Main Method Is Just a Regular Method
109
Command-Line Parameters
class TestMain {
public static void main(String[] args) {
...
}
}
110
Processing
Command-Line Parameters
111
Problem: Calculator
• Objective: Write a program that will perform
binary operations on integers. The program
receives three parameters: an operator and two
integers.
java Calculator 2 + 3
java Calculator 2 - 3
Calculator java Calculator 2 / 3
Run java Calculator 2 . 3
112