Lecture 16
Lecture 16
Introduction to Arrays
Adapted from:
1) Building Java Programs: A Back to Basics
Approach
by Stuart Reges and Marty Stepp
2) Runestone CSAwesome Curriculum
https://longbaonguyen.github.io
Can we solve this
problem?
• Consider the following program (input underlined):
3
Array declaration
type[] name = new type[length];
– Example:
int[] numbers = new int[10];
inde 0 1 2 3 4 5 6 7 8 9
x
value 0 0 0 0 0 0 0 0 0 0
4
Array declaration, cont.
• The length can be any integer expression.
int x = 2 * 3 + 1;
int[] data = new int[x % 5 + 2];
5
Accessing elements
name[index] // access
name[index] = value; // modify
– Example:
numbers[0] = 27;
numbers[3] = -6;
System.out.println(numbers[0]);
if (numbers[3] < 0) {
System.out.println("Element 3 is negative.");
}
inde 0 1 2 3 4 5 6 7 8 9
x
value 27
0 0 0 -6
0 0 0 0 0 0 0
6
Arrays of other types
double[] results = new double[5];
results[2] = 3.4;
results[4] = -0.5;
inde 0 1 2 3 4
x
value 0.0 0.0 3.4 0.0 -0.5
index 0 1 2 3 4
value null "hi" null "hello nul
" l
8
Arrays of other types
Point[] pts = new Point[5];
pts[1] = new Point(2, 3);
pts[4] = new Point();
index 0 1 2 3 4
value null null null
Point Point
x=2 x=0
y=3 y=0
• Example:
int[] data = new int[10];
System.out.println(data[0]); // okay
System.out.println(data[9]); // okay
System.out.println(data[-1]); // exception
System.out.println(data[10]); // exception
inde 0 1 2 3 4 5 6 7 8 9
x
value 0 0 0 0 0 0 0 0 0 0
10
Accessing array elements
int[] numbers = new int[8];
numbers[1] = 3;
numbers[4] = 99;
numbers[6] = 2;
int x = numbers[1];
numbers[x] = 42;
numbers[numbers[6]] = 11; // use numbers[6] as index
x 3
inde 0 1 2 3 4 5 6 7
x
numbers
value 0 3 11 42 99 0 2 0
11
Arrays and for loops
• It is common to use for loops to access array elements. This
is called traversing the array.
for (int i = 0; i < 8; i++) {
System.out.print(numbers[i] + " ");
}
System.out.println(); // output: 0 3 11 42 99 0 2 0
While Loop:
int sum = 0;
int i = 0;
while(i < numbers.length){
sum += numbers[i];
i++;
} Typically, traversing an array
For Loop: is best done with a for loop.
int sum = 0;
for(int i = 0; i < numbers.length; i++){
sum += numbers[i];
}
14
Quick array initialization
Arrays can be used created quickly using initializer
lists.
inde 0 1 2 3 4 5 6
x
valu 1 7 10 12 8 14 22
e
16
Limitations of arrays
• You cannot resize an existing array:
int[] a = new int[4];
a.length = 10; // error
17
Arrays.toString
public static void main(String[] args) {
int[] a = {0, 14, 4, 6, 8};
System.out.println(a);
}
Output: I@674f1c67 6. (14 pts)
Output:
a is [0, 14, 4, 6, 8]
19
Arrays
20
Arrays
21
Array Lab 1
Write the method average which accepts an int array and
returns the average of the values.
Write the method largest which accepts an int array and returns
the largest value of the array.
22
Array Lab 1
Also write the main method with an array and check to make
sure your methods work!
23
Array Lab 2
This lab is on Processing.
Methods:
update()
display()
25