Last time we looked at array temps:
temps
[0] [1] [2] [3] [4] [5] [6] [7] [8]
75 |
78 |
85 |
71 |
69 |
82 |
74 |
80 |
87 |
Using an array initializer:
int temps = {75, 78, 71, 69, 82, 74, 80, 87}; (Oops, one number missing)
1. Find the values for:
temps[4]
temps[temps.length – 1]
Here’s code from last time to sum up the array:
int sum = 0;
for (int i=0; i < temps.length;i++) {
sum += temps[i];
}
2. Write code to find the maximum temperature in the array—start with “int max = temps[0];” and replace as needed in the loop.
String[] days = {“Sun”, “Mon”, “Tue”, “Wed”, “Thu”, “Fri”, “Sat”};
[0] [1] [2] [3] [4] [5] [6]
Sun |
Mon |
Tue |
Wed |
Thu |
Fri |
Sat |
3. Find
days[5]
days[0] + “ “ + days[6]
4. Find the output for:
for (i=1; i<6; i++) {
System.out.print(days[i] + “ “);
}
String equality: Don’t forget to compare Strings with .equals:
if (s.equals(“xxx”)) … // check if s is “xxx”
5.
Write code to find the index of “Wed” in the array days by comparing each day’s String in turn. Set up a for loop over the array, and for each entry, check if it is equal to “Wed”, and if so, set variable index to its for-loop index. To be sure index has a proper value, set it to -1 to start.