 Here's part of a program that takes a week's worth of temperatures in degrees Celsius, calculates the average, and then determines how many of them are above average and how many are at or below average. I've already typed the part that defines seven variables, temperature one through temperature seven. Now I'll add them up and calculate the average. Then, to calculate the number of items above the average, I'll set n above to zero and then do this. If temperature one is greater than the average, add one to n above. Similarly, I can say if temperature two greater than average and above is incremented, and then I'll copy and paste it one, two, and make the changes for temperature three, temperature four, five, six, and seven. And now let's put in some code to display the output. The average temperature for the number of days, Celsius backslash n, seven days and our average, and the number of days above average, which is n above, and the number of days at or below the average, seven minus the number above. Let's save this and compile it and then let's run it. There's the average temperature, 22.8 degrees Celsius, three were above and four were below average. This program works, but what if we had a whole month's worth of temperatures? Then we'd have to have 30 repetitions of this if statement. We'd have to have 30 additions and oh boy, it would be a tremendous mess. There must be a better way. Let's think back to algebra, where we had a variable name with a subscript to represent a group of related values. And then we could use that subscripted variable in a summation expression. Now let's think about Java. Remember that we've treated variables like named mailboxes and each person has their own named mailbox. But if you go to an apartment building, you'll see one name and each mailbox will have an apartment number on it. We have a group of mailboxes all associated with one name. In a similar way, in Java, individual variables are like individual mailboxes. The equivalent of the apartment building mailbox group is an array. We use the square brackets after the data type to indicate that this is an array. And we can initialize the entries by listing them in braces. Unlike apartment numbers, which begin at one, the positions in an array start numbering at zero. And unlike algebra, where we use subscripts, we can't type subscripts in our editor, so we use square brackets to indicate the index or the mailbox number that we're accessing. With this knowledge, let's modify the program. First, I'm going to initialize the array with an alternate method. I'm going to declare the array as a double array named temperatures, and it will be a new double array of length seven. I don't need the word double on any of these anymore, and I'm going to change them to individually set each element of the array, starting at element zero, element one, all the way up to six. Now, instead of adding up all of the items in one expression, I'm going to do something that looks really weird. I'm going to set an index equal to zero, and I'm going to set my sum equal to zero. Then I'm going to set sum to sum plus temperatures at location index. That will be slot number zero, which will take zero plus 27.2, sum will be 27.2 when I'm done with this statement. I'll then add one to index to increment it. Now I'm going to do exactly the same thing again. This time index is one, which means it'll take the 27.2 and add temperatures sub one, which is 20, and sum will become 47.2. Then I'll do index plus plus. You're probably thinking, oh no, this is even worse than the original version. Is he actually going to copy and paste this five more times? No, I won't. When I see myself doing the same thing repetitively, I think, hey, can I do this with the loop? The answer is yes, I can. I'm going to say for index equals zero, index less than seven, index plus plus, sum becomes sum plus temperatures of index, and that is the same as if I had written this seven times. In the interest of good programming style, I'm going to make this int index, and then I don't need this line anymore. And now my average is the sum divided by seven. This works great, but if I ever decided to do 14 days or 30 days of temperatures, I'd have to change this seven to 14 or 30, and then I'd have to change this one and this one. When I initialize the array, I have to know how long it's going to be. And this one, instead of saying seven, I can say the number of items that are in the array. That's temperatures dot length. If I have 14 items, this will turn out to be 14. If I have 30 items, it'll be 30. Same thing here. I'm going to divide by the number of items in the array. Now I can do a similar loop to find the number of temperatures that are above average. I can say the number above is zero. And then for int index equals zero, index less than temperatures dot length index plus plus. If the temperatures at my current index is greater than the average, then the number above gets incremented. Let's do the output, the average temperature for days. And this time again, instead of seven, I'm going to say temperatures dot length and average. I can say number of days above average, and that's going to be an above, and the number of days at or below average is temperatures dot length. Minus the number above. Let's compile that and let's run it. And you'll see that it does exactly the same as the other one, except the program is a lot shorter. Again, because I'm using temperatures dot length everywhere, if I have more temperatures, my program is still going to work. Let me change to the brace initialization method and set the first seven to the way they are here and add a few more temperatures. Let's go up to 32.3, 31.8, 31.7, 30.0, 29.5, 29.5, and 29.3. I won't need these anymore, and everything else in my program is going to be exactly the same as it was before. Let's compile it and let's run it. And this time it gives me the average temperature for 14 days and calculates the number above and below. Arrays and for loops are a marriage made in heaven for dealing with multiple related values of data.