7 Array
7 Array
ARRAYS
Learning Outcome
At the end of this chapter, student should be able to:
Construct program from flowchart (C3, P4)
Construct program using array (C3, P4)
Explain the basic concepts and usage of array (C2,
A3)
Analyse and solve programming problem using
array(P3, CTPS)
2
Exercise
1. Write a program to calculate the average of five integer
numbers, then print the value of each number and their
average.
3
Without using Array
Q1 without using array
import java.util.Scanner;
public class NoArray {
public static void main(String[] args) {
int number; number
int sum = 0;
double average = 0.0 ;
import java.util.Scanner;
public class Array {
public static void main(String[] args) {
int[] number = new int[5]; number
int sum = 0;
double average = 0.0 ;
6
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
7
Declaring Array Variables
datatype[] arrayRefVar;
Example:
double[] myList;
8
Creating Arrays
arrayRefVar = new datatype[arraySize];
Example:
myList = new double[10];
myList[0] references the first element in the array.
myList[9] references the last element in the array.
9
Declaring and Creating
in One Step
datatype[] arrayRefVar = new datatype[arraySize];
10
The Length of an Array
Once an array is created, its size is fixed. It cannot be
changed.
For example,
myList.length returns 10
11
Default Values
When an array is created, its elements are
assigned the default value of:
12
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 previous example, myList
holds ten double values and the indices are from 0 to 9.
arrayRefVar[index];
e.g. myList[8];
13
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].
14
Array Initializers
Declaring, creating, initializing in one step:
double[] myList = {1.9, 2.9, 3.4, 3.5};
15
Declaring, Creating, Initializing
using the Shorthand Notation
double[] myList = {1.9, 2.9, 3.4, 3.5};
17
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) After the array is created
{ values
int[] values = new int[5];
0 0
for (int i = 1; i < 5; i++) {
values[i] = i + values[i-1]; 1 0
} 2 0
values[0] = values[1] + values[4];
3 0
}
} 4 0
18
animation
Trace Program with Arrays
i becomes 1
i 1
public class Test {
public static void main(String[] args)
{ values
int[] values = new int[5];
0 0
for (int i = 1; i < 5; i++) {
values[i] = i + values[i-1]; 1 0
} 2 0
values[0] = values[1] + values[4];
3 0
}
} 4 0
19
animation
Trace Program with Arrays
i (=1) is less than 5
i 1
public class Test {
public static void main(String[] args)
{ values
int[] values = new int[5];
0 0
for (int i = 1; i < 5; i++) {
values[i] = i + values[i-1]; 1 0
} 2 0
values[0] = values[1] + values[4];
3 0
}
} 4 0
20
animation
Trace Program with Arrays
After this line is executed, value[1] is 1
i 1
public class Test {
public static void main(String[] args) After the first iteration
{ values
int[] values = new int[5];
0 0
for (int i = 1; i < 5; i++) {
values[i] = i + values[i-1]; 1 1
} 2 0
values[0] = values[1] + values[4];
3 0
}
} 4 0
21
animation
22
animation
Trace Program with Arrays
i (= 2) is less than 5 i 2
public class Test {
public static void main(String[] args) After the first iteration
{ values
int[] values = new int[5];
0 0
for (int i = 1; i < 5; i++) {
values[i] = i + values[i-1]; 1 1
} 2 0
values[0] = values[1] + values[4];
3 0
}
} 4 0
23
animation
Trace Program with Arrays
After this line is executed,
values[2] is 3 (2 + 1) i 2
public class Test {
public static void main(String[] args) After the second iteration
{ values
int[] values = new int[5];
0 0
for (int i = 1; i < 5; i++) {
values[i] = i + values[i-1]; 1 1
} 2 3
values[0] = values[1] + values[4];
3 0
}
} 4 0
24
animation
Trace Program with Arrays
After this, i becomes 3.
i 3
public class Test {
public static void main(String[] args) After the second iteration
{ values
int[] values = new int[5];
0 0
for (int i = 1; i < 5; i++) {
values[i] = i + values[i-1]; 1 1
} 2 3
values[0] = values[1] + values[4];
3 0
}
} 4 0
25
animation
Trace Program with Arrays
i (=3) is still less than 5.
i 3
public class Test {
public static void main(String[] args) After the second iteration
{ values
int[] values = new int[5];
0 0
for (int i = 1; i < 5; i++) {
values[i] = i + values[i-1]; 1 1
} 2 3
values[0] = values[1] + values[4];
3 0
}
} 4 0
26
animation
Trace Program with Arrays
After this line, values[3] becomes 6 (3 + 3)
i 3
public class Test {
public static void main(String[] args) After the third iteration
{ values
int[] values = new int[5];
0 0
for (int i = 1; i < 5; i++) {
values[i] = i + values[i-1]; 1 1
} 2 3
values[0] = values[1] + values[4];
3 6
}
} 4 0
27
animation
Trace Program with Arrays
After this, i becomes 4
i 4
public class Test {
public static void main(String[] args) After the third iteration
{ values
int[] values = new int[5];
0 0
for (int i = 1; i < 5; i++) {
values[i] = i + values[i-1]; 1 1
} 2 3
values[0] = values[1] + values[4];
3 6
}
} 4 0
28
animation
Trace Program with Arrays
i (=4) is still less than 5
i 4
public class Test {
public static void main(String[] args) After the third iteration
{ values
int[] values = new int[5];
0 0
for (int i = 1; i < 5; i++) {
values[i] = i + values[i-1]; 1 1
} 2 3
values[0] = values[1] + values[4];
3 6
}
} 4 0
29
animation
Trace Program with Arrays
After this, values[4] becomes 10 (4 + 6)
i 4
public class Test {
public static void main(String[] args) After the fourth iteration
{ values
int[] values = new int[5];
0 0
for (int i = 1; i < 5; i++) {
values[i] = i + values[i-1]; 1 1
} 2 3
values[0] = values[1] + values[4];
3 6
}
} 4 10
30
animation
Trace Program with Arrays
After i++, i becomes 5
i 5
public class Test {
public static void main(String[] args) After the fourth iteration
{ values
int[] values = new int[5];
0 0
for (int i = 1; i < 5; i++) {
values[i] = i + values[i-1]; 1 1
} 2 3
values[0] = values[1] + values[4];
3 6
}
} 4 10
31
animation
i 5
public class Test {
public static void main(String[] args) After the fourth iteration
{ values
int[] values = new int[5];
0 0
for (int i = 1; i < 5; i++) {
values[i] = i + values[i-1]; 1 1
} 2 3
values[0] = values[1] + values[4];
3 6
}
} 4 10
32
animation
Trace Program with Arrays
After this line, values[0] is 11 (1 + 10)
33
Processing Arrays
See the examples in the text.
1. (Initializing arrays)
2. (Printing arrays)
3. (Summing all elements)
4. (Finding the largest element)
5. (Finding the smallest index of the largest element)
34
Initializing Arrays
Initializing arrays with random values.
35
Initializing arrays with input values
36
Printing Arrays
for (int i = 0; i < myList.length; i++)
{
System.out.println(myList[i] + );
}
37
Summing all elements
double total = 0;
for (int i = 0; i < myList.length; i++) {
total += myList[i];
}
38
Finding the largest element
double max = myList[0];
for (int i = 1; i < myList.length; i++)
{
if(myList[i] > max)
max = myList[i];
}
39
Finding the smallest index
of the largest element
40
Example: Testing Arrays
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, therefore the
largest number is 5 and its occurrence count is 4.
41
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
42
Copying Arrays
Using a loop:
int[] sourceArray = {2, 3, 1, 5, 10};
int[] targetArray = new int[sourceArray.length];
43
The arraycopy Utility
arraycopy(sourceArray, src_pos,
targetArray, tar_pos, length);
Examples:
System.arraycopy(sourceArray, 0,
targetArray, 0, sourceArray.length);
System.arraycopy(sourceArray, 2,
targetArray, 0, 3);
44
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:
int[] list = {3, 1, 2, 6, 4, 2};
printArray(list);
Anonymous array
45
Passing Arrays to Methods
public static void main(String[] args) {
int[] list = {3, 1, 2, 6, 4, 2};
printArray(list);
}
46
Passing Arrays to Methods
public static void main(String[] args) {
printArray(new int[]{3, 1, 2, 6, 4, 2});
}
47
Anonymous Array
The statement
printArray(new int[]{3, 1, 2, 6, 4, 2});
48
Pass By Value
Java uses pass by value to pass parameters to a
method.
There are important differences between passing a
value of variables of primitive data types and
passing arrays.
49
Pass By Value
For a parameter of a primitive type value, the actual
value is passed.
Changing the value of the local parameter inside the
method does not affect the value of the variable outside
the method.
For a parameter of an array type, the value of the
parameter contains a reference to an array; this
reference is passed to the method.
Any changes to the array that occur inside the method
body will affect the original array that was passed as the
argument.
50
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
51
Returning an Array from a Method
public static int[] reverse(int[] list) {
int[] result = new int[list.length];
return result;
}
53
animation
return result;
}
list 1 2 3 4 5 6
result 0 0 0 0 0 0
54
animation
return result;
}
list 1 2 3 4 5 6
result 0 0 0 0 0 0
55
animation
return result;
}
list 1 2 3 4 5 6
result 0 0 0 0 0 0
56
animation
return result;
}
list 1 2 3 4 5 6
result 0 0 0 0 0 1
57
animation
return result;
}
list 1 2 3 4 5 6
result 0 0 0 0 0 1
58
animation
return result;
}
list 1 2 3 4 5 6
result 0 0 0 0 0 1
59
animation
return result;
}
list 1 2 3 4 5 6
result 0 0 0 0 2 1
60
animation
return result;
}
list 1 2 3 4 5 6
result 0 0 0 0 2 1
61
animation
return result;
}
list 1 2 3 4 5 6
result 0 0 0 0 2 1
62
animation
return result;
}
list 1 2 3 4 5 6
result 0 0 0 3 2 1
63
animation
return result;
}
list 1 2 3 4 5 6
result 0 0 0 3 2 1
64
animation
return result;
}
list 1 2 3 4 5 6
result 0 0 0 3 2 1
65
animation
return result;
}
list 1 2 3 4 5 6
result 0 0 4 3 2 1
66
animation
return result;
}
list 1 2 3 4 5 6
result 0 0 4 3 2 1
67
animation
return result;
}
list 1 2 3 4 5 6
result 0 0 4 3 2 1
68
animation
return result;
}
list 1 2 3 4 5 6
result 0 5 4 3 2 1
69
animation
return result;
}
list 1 2 3 4 5 6
result 0 5 4 3 2 1
70
animation
return result;
}
list 1 2 3 4 5 6
result 0 5 4 3 2 1
71
animation
return result;
}
list 1 2 3 4 5 6
result 6 5 4 3 2 1
72
animation
return result;
}
list 1 2 3 4 5 6
result 6 5 4 3 2 1
73
animation
return result;
}
list 1 2 3 4 5 6
result 6 5 4 3 2 1
74
animation
return result;
}
1 2 3 4 5 6
list
list2
result 6 5 4 3 2 1
75
Two-dimensional Arrays
// Declare array ref var
dataType[][] refVar;
// Alternative syntax
dataType refVar[][] = new dataType[10][10];
76
Declaring Variables of Two-
dimensional Arrays and Creating
Two-dimensional Arrays
double[][] x;
77
Two-dimensional Array
Illustration
0 1 2 3 4 0 1 2 3 4 0 1 2
0 0 0 1 2 3
1 1 1 4 5 6
2 2 7 2 7 8 9
3 3 3 10 11 12
4 4 int[][] array = {
{1, 2, 3},
matrix = new int[5][5]; matrix[2][1] = 7; {4, 5, 6},
{7, 8, 9},
{10, 11, 12}
};
matrix.length? 5
array.length? 4
matrix[0].length? 5
array[0].length? 3 78
Declaring, Creating, and Initializing
Using Shorthand Notations
You can also use an array initializer to declare,
create and initialize a two-dimensional array. For
example,
int[][] array = {
int[][] array = new int[4][3];
{1, 2, 3}, array[0][0] = 1; array[0][1] = 2; array[0][2] = 3;
{4, 5, 6}, Same as array[1][0] = 4; array[1][1] = 5; array[1][2] = 6;
{7, 8, 9}, array[2][0] = 7; array[2][1] = 8; array[2][2] = 9;
{10, 11, 12} array[3][0] = 10; array[3][1] = 11; array[3][2] = 12;
};
79
Lengths of Two-Dimensional
Arrays
int[][] x = new int[3][4];
x
x[0][0] x[0][1] x[0][2] x[0][3] x[0].length is 4
x[0]
x[1] x[1][0] x[1][1] x[1][2] x[1][3] x[1].length is 4
x[2]
x[2][0] x[2][1] x[2][2] x[2][3] x[2].length is 4
x.length is 3
80
Lengths of Two-Dimensional
Arrays, cont.
int[][] array = { array.length
{1, 2, 3}, array[0].length
{4, 5, 6}, array[1].length
{7, 8, 9}, array[2].length
{10, 11, 12}
array[3].length
};
array[4].length ArrayIndexOutOfBoundsException
81
Example: Initializing Arrays with
Random Values
int[][] matrix = new int[10][10];
82
Example: Printing Arrays
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[i].length; j++)
{
System.out.print(matrix[i][j] + );
}
System.out.println();
}
83
Example: Summing all elements
int total = 0;
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[i].length; j++)
{
total = total + matrix[i][j];
}
}
System.out.println(Total all elements:
+total);
84
Example: Grading Multiple-
Choice Test
Objective: write a program that grades
multiple-choice test.
Students Answers to the Questions:
0 1 2 3 4 5 6 7 8 9
85
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
matrix[0].length is 5
{2, 3, 4, 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
};
86
Ragged Arrays, cont.
int[][] triangleArray = { 1 2 3 4 5
{1, 2, 3, 4, 5},
{2, 3, 4, 5}, 2 3 4 5
{3, 4, 5},
{4, 5}, 3 4 5
{5}
}; 4 5
5
87