7.1 Array Basics

And in such indexes, although small pricks

To their subsequent volumes, there is seen

The baby figure of the giant mass

Of things to come.

—WILLIAM SHAKESPEARE, Troilus and Cressida

Suppose you want to compute the average temperature for the seven days in a week. You might use the following code:

Scanner keyboard = new Scanner(System.in);
System.out.println(“Enter 7 temperatures:”);
double sum = 0;
for (int count = 0; count < 7; count++)
{
    double next = keyboard.nextDouble();
    sum = sum + next;
}
double average = sum / 7;

This works fine if all you want to know is the average. But let’s say you also want to know which temperatures are above and which are below the average. Now you have a problem. In order to compute the average, ...

Get Java: An Introduction to Problem Solving and Programming, 8th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.