L08 Arrays
L08 Arrays
Arrays
• An array is a set of variables of the same type
accessed by their index
31 28 31 30
day[0] day[2]
day[1] day[3]
2
1
Arrays (II)
• The previous example creates 4 integers.
– They are just accessed by their position
• E,g, day[0], day[1], …
– Each integer has its own value
• Arrays can be of any type
– int, double, char, boolean, String…
– Every element of an array has the same type
• Arrays are created using the new keyword
– type[] name = new type[size];
– The new command allocates a block of memory
• The length field of an array tells you how many elements it
has
– E.g., day.length == 4.
3
2
Step 1: Decomposition
• What has to be done?
• Initialize counters! – another loop
– Read strings from terminal
– For each string, Nested
• For each character, loops
– Increment counter for that character
Steps 2-4:
• Tackle each step individually
– Initialize counters
• Declare the counters first
• But there are 26 of them…
• … so use an array!
3
Step 2 (continued)
– Still initializing counters…
• The counters count how often each letter appears
• So we need to initialize them to zero… all 26 of
them!
• So we use a loop… a for loop (26 iterations)
4
Step 2 (one more time!)
• Actually, hard-coding ’26’ is a bad idea
– What if we want to include capitals later?
– What if we want to re-use this code?
• We want to initialize the whole array, and nothing more:
Does it work?
• Did we declare and initialize the counters correctly?
– Never assume something is correct
– Test each step before moving on
• How?
– Method #1: Use the debugger
• Run the code (so far) step-by-step (F6)
• Check that the array has 26 elements
• Check that each is set to zero
– Method #2: Write the code to print counters
• Test it before you start counting letters
10
5
Printing the Counters
11
// Print Counters
for(int i=0; i < 26; i++) {
System.out.print(alpha_counter[i]+” ”);
}
System.out.println();
12
6
Running Your First Test
Class Letters {
public static void main(String[] args) {
// Declare and initialize counters
int[] alpha_counter = new int[26];
for(int i=0; i < 26; i++) {
alpha_counter[i] = 0;
}
// Print Counters
for(int i=0; i < 26; i++) {
System.out.print(alpha_counter[i]+” ”);
}
System.out.println();
}
}
13
14
7
One approach…
• We’ve already written a loop to read until “quit”
– Remember the Echo program?
• So let us start with that:
15
8
Counting Letters
• Given a string, we need to increment a counter
for every letter.
– Nested loop inside the get string loop
17
18
9
Counting Letters (alt)
• The previous code is clean but long.
• Java supports ASCII char subtraction
char first_char = ‘a’;
char second_char = ‘b’;
// z == 1
int z = second_char – first_char;
20
10
All together (II)
// count letters
Scanner in_str = new Scanner(System.in);
String user_string = in_str.next();
while (!user_string.equals("quit")) {
for(int i=0; i < user_string.length(); i++) {
char letter = user_string.charAt(i);
int offset = letter – ‘a’;
if ((offset >= 0) && (offset < 26)) {
alpha_counter[offset]++;
}
}
user_string = in_str.next();
}
21
22
11
Done Yet? No!
• Test your program:
– Make up some input
– Count the letters by hand
– Double check your program’s results
• Make Hard Tests
– Include characters that aren’t letters
– Test the case where the first input is ‘quit’
– Test really long inputs
– ….
• Hint: it can be good to think of test cases first.
23
Methodology Review
24
12