Transcript 07a-arrays1

CSE 142 Lecture Notes
Arrays
Suggested reading: 7.1 - 7.2.1, 7.4 - 7.5
Suggested self-checks: Section 7.11 #1-11
These lecture notes are copyright (C) Marty Stepp 2005. May not be rehosted, copied, sold, or
modified without Marty Stepp's expressed written permission. All rights reserved.
1
Too many values...

How would we implement a program such as this?
How many numbers: 5
Number 1: 2
Number 2: 7
Number 3: 17
Number 4: -1
Number 5: 64
Here are the numbers you typed:
Number 1: 2
Number 2: 7
Number 3: 17
Number 4: -1
Number 5: 64

We don't know how many int variables we should
declare in advance... it could be very many!
2
Arrays

array: A single variable that can store many values of
the same type.



element: One value in an array.
index: A 0-based integer, which is used to access an element
from an array.
We usually draw arrays as horizontal grids.

An array of integers:
index
0
1
2
3
value 12 49 -2 26
4
5
5
6
7
8
17 -6 84 72
9
3
3
Array declaration

Declaring/initializing an array, general syntax:
<type> [] <name> = new <type> [ <length> ];



The length of the array is specified between [ ] brackets.
Example:
int[] numbers = new int[10];
index
0
1
2
3
4
5
6
7
8
9
value
0
0
0
0
0
0
0
0
0
0
Example:
double[] grades = new double[6];
index 0
1
2
3
4
5
value 0.0 0.0 0.0 0.0 0.0 0.0
4
Accessing array elements

Assigning a value to an array element, general syntax:
<array name> [ <index> ] = <value> ;
 Example:
numbers[0] = 27;
numbers[3] = -1;
index
0
value 27

1
2
3
4
5
6
7
8
9
0
0
-1
0
0
0
0
0
0
When arrays are initially constructed, every element
has a 0-equivalent value.





int: 0
double: 0.0
boolean: false
char: '\0' (the "null character")
String or other object: null (null means "no object")
5
Arrays and for loops

Arrays are very commonly used with for loops that
pass over each element and process it in some way:



Example (print each element of an array):
for (int i = 0; i < 10; i++) {
System.out.print(numbers[i] + " ");
}
Output:
27 0 0 -1 0 0 0 0 0 0
Example (store squared numbers in an array):
for (int i = 0; i < 10; i++) {
numbers[i] = i * i;
}
index
0
1
2
3
value
0
1
4
9
4
5
6
7
8
9
16 25 36 49 64 81
6
The .length field

An array has a field named .length that returns its
total number of elements.



This is preferred to be used instead of hard-coding the array's
length as a literal value. (Why?)
Example:
for (int i = 0; i < numbers.length; i++) {
System.out.print(numbers[i] + " ");
}
Output:
0 1 4 9 16 25 36 49 64 81
7
Array initialization shortcut

Quick array initialization, general syntax:
<type> [] <name> = {<value>, <value>, ..., <value>};

Example:
int[] numbers = {12, 49, -2, 26, 5, 17, -6, 84, 72, 3};
index
0
1
2
3
value 12 49 -2 26

4
5
5
6
7
8
17 -6 84 72
9
3
This syntax is useful when you know in advance what the
array's elements will be.
8
Array practice problem

What are the contents of the array a after the following
code?
int[] a = {2, 5, 1, 6, 14, 7, 9};
for (int i = 1; i < a.length; i++) {
a[i] += a[i - 1];
}
index
0
1
2
3
4
5
6
value
9
Limitations of arrays

An array cannot be directly printed by println:



Example:
int[] numbers = new int[10];
System.out.println(numbers);
Output:
[I@1cb52ae
Arrays cannot be compared correctly with == or equals:


Example:
int[] numbers1 = new int[10];
int[] numbers2 = new int[10];
System.out.println(numbers1 == numbers2);
Output:
false
10
More array limitations

An array's size cannot be modified after it is created.
The .length field is read-only; you cannot change it.
 Example:
int[] numbers = new int[10];
numbers.length = 20; // does not compile!



You can instead create a new larger array and copy the
elements over into it.
It is illegal to access an array element with an index
that is less than 0 or greater than its .length - 1.
If you do so, your program will crash with an exception.
 Example:
int[] numbers = new int[10];
System.out.println(numbers[10]); // kaboom!

11
Why are arrays useful?

We can use arrays to store a large amount of data
without declaring many variables.


Arrays can help us to group related data into elements.


Example: Read in a file of 1000 numbers, then print out the
numbers in reverse order.
Example: For a given school exam, open a file full of exam
scores and count up how many students got each score from 0
through 100.
Arrays let us hold on to data and access it in random
order.

Example: Read a file full of babies' names, store each name's
data as an element in a large array, and then examine it later
to find names that the user types.
12
Arrays as parameters

An array can be passed as a parameter to a method.


Example:
int[] iq = new int[3];
iq[0] = 126;
iq[1] = 167;
iq[2] = 95;
int max = getMaxValue(iq);
System.out.println("Max = " + max);
...
public static int getMaxValue(int[] array) {
int max = array[0];
for (int i = 1; i < array.length; i++) {
if (array[i] > max) {
max = array[i];
}
}
return max;
}
Output:
Max = 167
13
Arrays as parameters, contd.

When an array is passed as a parameter, it is passed as
a reference (similar to objects).



If the method modifies elements of its array parameter, the
changes will also be seen in the original array.
Example:
int[] iq = new int[3];
iq[0] = 126;
iq[1] = 167;
iq[2] = 95;
destroy(iq);
System.out.println(array[0] + " " + array[1]);
...
public static void destroy(int[] array) {
for (int i = 0; i < array.length; i++) {
array[i] = 0; // ha ha!
}
}
Output:
0 0
14
Array practice problems

Write a method named print that accepts an array of
integers as its parameter and prints the elements of
the array in the following format:


{7, -2, 46, 3, 55}
Write a method named equal that accepts 2 arrays of
integers as its parameters and returns whether those
two arrays contain exactly the same elements in the
same order.
15