19.09.2015 Views

Prentice.Hall.Introduction.to.Java.Programming,.Brief.Version.9th.(2014).[sharethefiles.com]

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

224 Chapter 6 Single-Dimensional Arrays<br />

problem<br />

why array?<br />

what is array?<br />

create array<br />

s<strong>to</strong>re number in array<br />

get average<br />

above average?<br />

index<br />

Key<br />

Point<br />

Key<br />

Point<br />

6.1 <strong>Introduction</strong><br />

A single array variable can reference a large collection of data.<br />

Often you will have <strong>to</strong> s<strong>to</strong>re a large number of values during the execution of a program.<br />

Suppose, for instance, that you need <strong>to</strong> read 100 numbers, <strong>com</strong>pute their average, and find out<br />

how many numbers are above the average. Your program first reads the numbers and <strong>com</strong>putes<br />

their average, then <strong>com</strong>pares each number with the average <strong>to</strong> determine whether it is above<br />

the average. In order <strong>to</strong> ac<strong>com</strong>plish this task, the numbers must all be s<strong>to</strong>red in variables. You<br />

have <strong>to</strong> declare 100 variables and repeatedly write almost identical code 100 times. Writing a<br />

program this way would be impractical. So, how do you solve this problem?<br />

An efficient, organized approach is needed. <strong>Java</strong> and most other high-level languages<br />

provide a data structure, the array, which s<strong>to</strong>res a fixed-size sequential collection of elements<br />

of the same type. In the present case, you can s<strong>to</strong>re all 100 numbers in<strong>to</strong> an array and access<br />

them through a single array variable. The solution may look like this:<br />

1 public class AnalyzeNumbers {<br />

2 public static void main(String[] args) {<br />

3 final int NUMBER_OF_ELEMENTS = 100;<br />

4 double[] numbers = new double[NUMBER_OF_ELEMENTS];<br />

5 double sum = 0;<br />

6<br />

7 java.util.Scanner input = new java.util.Scanner(System.in);<br />

8 for (int i = 0; i < NUMBER_OF_ELEMENTS; i++) {<br />

9 System.out.print("Enter a new number: ");<br />

10<br />

11<br />

numbers[i] = input.nextDouble();<br />

sum += numbers[i];<br />

12 }<br />

13<br />

14 double average = sum / NUMBER_OF_ELEMENTS;<br />

15<br />

16 int count = 0; // The number of elements above average<br />

17 for (int i = 0; i < NUMBER_OF_ELEMENTS; i++)<br />

18 if (numbers[i] > average)<br />

19 count++;<br />

20<br />

21 System.out.println("Average is " + average);<br />

22 System.out.println("Number of elements above the average "<br />

23 + count);<br />

24 }<br />

25 }<br />

numbers<br />

numbers[0]:<br />

numbers[1]:<br />

numbers[2]:<br />

array<br />

.<br />

numbers[i] .<br />

.<br />

numbers[97]:<br />

numbers[98]:<br />

numbers[99]:<br />

The program creates an array of 100 elements in line 4, s<strong>to</strong>res numbers in<strong>to</strong> the array in line<br />

10, adds each number <strong>to</strong> sum in line 11, and obtains the average in line 14. It then <strong>com</strong>pares<br />

each number in the array with the average <strong>to</strong> count the number of values above the average<br />

(lines 16–19).<br />

This chapter introduces single-dimensional arrays. The next chapter will introduce twodimensional<br />

and multidimensional arrays.<br />

6.2 Array Basics<br />

Once an array is created, its size is fixed. An array reference variable is used <strong>to</strong> access<br />

the elements in an array using an index.<br />

An array is used <strong>to</strong> s<strong>to</strong>re a collection of data, but often we find it more useful <strong>to</strong> think of an array<br />

as a collection of variables of the same type. Instead of declaring individual variables, such as<br />

number0, number1, . . . , and number99, you declare one array variable such as numbers and<br />

use numbers[0], numbers[1], . . . , and numbers[99] <strong>to</strong> represent individual variables.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!