10.3 One-Dimensional Arrays | 485
// Read 1,000 numbers and print them in reverse order
importjava.io.*;
public classArrayExample
{
public static voidmain(String[] args) throwsIOException
{
BufferedReader inFile;
PrintWriter outFile;
inFile = new BufferedReader(new FileReader("infile.dat"));
outFile = new PrintWriter(new FileWriter("outfile.dat"));
// Declare and instantiate an array
int[] value = new int[1000];
for (int number = 0; number < 1000; number++)
value[number] = Integer.parseInt(inFile.readLine());
for (int number = 999; number >= 0; number--)
outFile.println(value[number]);
inFile.close();
outFile.close();
}
}
infile outfile
10 9999
20 9998
30 9997
40.
..
.40
9997 30
9998 20
9999 10
In general terminology, an array differs from a class in three fundamental ways:
1.An array is a homogeneousdata structure (all components in the structure are of
the same data type), whereas classes are heterogeneoustypes (their components
may be of different types).
2.A component of an array is accessed by its positionin the structure, whereas a
component of a class is accessed by an identifier(the field name).
3.Because array components are accessed by position, an array is a structured
data type.