Java Reference
In-Depth Information
6.1 Introduction
A single array variable can reference a large collection of data.
Key
Point
Often you will have to store a large number of values during the execution of a program.
Suppose, for instance, that you need to read 100 numbers, compute their average, and find out
how many numbers are above the average. Your program first reads the numbers and computes
their average, then compares each number with the average to determine whether it is above
the average. In order to accomplish this task, the numbers must all be stored in variables. You
have to declare 100 variables and repeatedly write almost identical code 100 times. Writing a
program this way would be impractical. So, how do you solve this problem?
An efficient, organized approach is needed. Java and most other high-level languages
provide a data structure, the array , which stores a fixed-size sequential collection of elements
of the same type. In the present case, you can store all 100 numbers into an array and access
them through a single array variable. The solution may look like this:
problem
why array?
what is array?
1
public class AnalyzeNumbers {
2
public static void main(String[] args) {
numbers
array
3
final int NUMBER_OF_ELEMENTS = 100 ;
create array
double [] numbers = new double [NUMBER_OF_ELEMENTS];
4
5
numbers[0]:
numbers[1]:
numbers[2]:
double sum = 0 ;
6
7 java.util.Scanner input = new java.util.Scanner(System.in);
8 for ( int i = 0 ; i < NUMBER_OF_ELEMENTS; i++) {
9 System.out.print( "Enter a new number: " );
10
11 sum += numbers[i];
12 }
13
14
.
numbers[i]
.
.
numbers[i] = input.nextDouble();
store number in array
numbers[97]:
numbers[98]:
numbers[99]:
get average
double average = sum / NUMBER_OF_ELEMENTS;
15
16 int count = 0 ; // The number of elements above average
17 for ( int i = 0 ; i < NUMBER_OF_ELEMENTS; i++)
18 if (numbers[i] > average)
19 count++;
20
21 System.out.println( "Average is " + average);
22 System.out.println( "Number of elements above the average "
23 + count);
24 }
25 }
above average?
The program creates an array of 100 elements in line 4, stores numbers into the array in line
10, adds each number to sum in line 11, and obtains the average in line 14. It then compares
each number in the array with the average to count the number of values above the average
(lines 16-19).
This chapter introduces single-dimensional arrays. The next chapter will introduce two-
dimensional and multidimensional arrays.
6.2 Array Basics
Once an array is created, its size is fixed. An array reference variable is used to access
the elements in an array using an index .
Key
Point
index
An array is used to store a collection of data, but often we find it more useful to think of an array
as a collection of variables of the same type. Instead of declaring individual variables, such as
number0 , number1 , . . . , and number99 , you declare one array variable such as numbers and
use numbers[0] , numbers[1] , . . . , and numbers[99] to represent individual variables.
 
 
 
 
Search WWH ::




Custom Search