15-Arrays
15-Arrays
Chris Piech
CS106A, Stanford University
Changing Variable Types
int to double? int to String?
int x = 5; int x = 5;
double xDbl = x; String xStr = “” + x
int to char
int diff = ‘C'-’A’;
char next = (char)‘a’ + diff;
Where are we?
´
Returns the number of values in the list.
list.isEmpty()
Returns true if the list is empty.
list.set(i, value)
array[i] = value
Sets the ith entry in the list to value.
list.get(i)
array[i]
´
Returns the ith entry in the list.
list.add(value)
´
Adds a new value to the end of the list.
list.add(index, value)
´
Inserts the value before the specified index position.
list.remove(index)
´
Removes the value at the specified index position.
list.clear()
Removes all values from the list.
Why use arrays?
• Arrays are built into the Java language and offer a more
expressive selection syntax.
• You can create arrays of primitive types like int and double
and therefore don’t need to use wrapper types like Integer
and Double.
• It is much easier to create arrays of a fixed, predetermined
size.
• Java makes it easy to initialize the elements of an array.
• Many methods in the Java libraries take arrays as parameters
or return arrays as a result. You need to understand arrays in
order to use those methods.
1. Fantastic style
53‡‡†305))6*;4826)4‡•)4‡);806*;48†8¶
60))85;1‡(;:‡*8†83(88)5*†;46(;88*96*
?;8)*‡(;485);5*†2:*‡(;4956*2(5*–4)8¶
8*;4069285);)6†8)4‡‡;1(‡9;48081;8:8‡
1;48†85;4)485†528806*81(‡9;48;(88;4(
‡?34;48)4‡;161;:188;‡?;
Arrays
index 0 1 2 3 4 5 6 7 8 9
length = 10
value 12 49 -2 26 5 17 -6 84 72 3
index 0 1 2 3 4
value 0 0 0 0 0
index 0 1 2 3 4 5 6
value 0 1 2 3 4 5 6
Setting values
Putting Data In An Array
// 0 1 2 3 4 5 6 7
A. {0, 3, 0, 2, 7, 0, 5, 9}
B. {0, 3, 0, 0, 7, 0, 5, 0}
C. {3, 3, 5, 2, 7, 4, 5, 0}
D. {0, 3, 0, 2, 7, 6, 4, 4}
Many flavors of arrays
Arrays Of Other Types
You can create arrays of any variable type. For example:
myArray.length
Prac%ce:
• What is the index of the last element of an array in terms
of its length?
• What is the index of the middle element of an array in
terms of its length?
Arrays ❤ loops
value 0 2 4 6 8 10 12 14
Arrays ❤ loops
// This is tedious!
Fancy ini%aliza%on
Brief Aside: Creating Arrays
Some%mes, we want to hardcode the elements of an array.
Luckily, Java has a special syntax for ini%alizing arrays to
hardcoded numbers.
Output:
e is [0, 14, 4, 6, 8]
Arrays as Parameters
Passing Arrays Between Methods
• Arrays are just another variable type, so methods can take
arrays as parameters and return an array.
31
Arrays: Pass By Reference
public void run() {
int[] numbers = new int[7];
fillArray(numbers);
println(Arrays.toString(numbers));
}
0
9 1
80 270 063 054 540 630 7
20 8
10 09
0 1 2 3 4 5 6 7 8 9
ReverseArray
Enter number of elements: 10
Forward: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Reverse: [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
skip simulation
Cryptogram
• A cryptogram is a puzzle in which a message is encoded by
replacing each letter in the original text with some other letter.
The substitution pattern remains the same throughout the
message. Your job in solving a cryptogram is to figure out
this correspondence.
• One of the most famous cryptograms was
written by Edgar Allan Poe in his short
story “The Gold Bug.”
• In this story, Poe describes the technique
of assuming that the most common letters
in the coded message correspond to the
most common letters in English, which
are E, T, A, O, I, N, S, H, R, D, L, and U.
Edgar Allan Poe (1809-1849)
Poe’s Cryptographic Puzzle
53‡‡†305))6*;4826)4‡•)4‡);806*;48†8¶ 8 33
60))85;1‡(;:‡*8†83(88)5*†;46(;88*96* ; 26
?;8)*‡(;485);5*†2:*‡(;4956*2(5*–4)8¶ 4 19
‡ 16
8*;4069285);)6†8)4‡‡;1(‡9;48081;8:8‡ ) 16
1;48†85;4)485†528806*81(‡9;48;(88;4( * 13
‡?34;48)4‡;161;:188;‡?; 5 12
6 11
( 10
† 8
1 8
0 6
9 5
AG
5 3O
‡O‡D
†G3L
0A5S
)S)I
6N*T
;H4E
8B2I
6S)H
4O‡P
•S)H
4O‡S
)T;E
8L0I
6N*T
;H4E
8D†E
8V¶ 2 5
: 4
6L
I 0S
)S)E
8A5T
;F1O
‡R(T
;Y:O
‡N*E
8D†E
8G3R
(E8E
8S)A
5N*D
†T;H
4I6R
(T;E
8E8N
*M9I
6N* 3 4
?T
U ;E
8S)N
*O‡R
(T;H
4E8A
5S)T
;A5N
*D†B
2Y:N
*O‡R
(T;H
4M9A
5I6N
*B2R
(A5N
*C–H
4S)E
8V¶ ? 3
8N
E *T
;H4L
0I6M
9B2E
8A5S
)T;S
)I6D
†E8S
)H4O
‡O‡T
;F1R
(O‡M
9T;H
4E8L
0E8F
1T;E
8Y:E
8O‡ ¶ 2
1T
F ;H
4E8D
†E8A
5T;H
4S)H
4E8A
5D†A
5B2E
8E8L
0I6N
*E8F
1R(O
‡M9T
;H4E
8T;R
(E8E
8T;H
4R( – 1
‡U
O ?G
3H4T
;H4E
8S)H
4O‡T
;F1I
6F1T
;Y:F
1E8E
8T;O
‡U?T
; • 1
Implementa)on Strategy
The basic idea behind the program to count letter frequencies is
to use an array with 26 elements to keep track of how many times
each letter appears. As the program reads the text, it increments
the array element that corresponds to each letter.
X [ A W B V MP P I K
10 10 0 0 0 0 10 0 210 0 0 120 0 0 0 0 0 1
0 1
0 1
0 0 0 1
0 0 0 0
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
Putting Data In An Array
To the code!
Count Le)er Frequencies
import acm.program.*;
/**
* This program creates a table of the letter frequencies in a
* paragraph of input text terminated by a blank line.
*/
public class CountLetterFrequencies extends ConsoleProgram {
/* Private instance variables */
private int[] frequencyTable;