One Dimensional Arrays
An array is a collection of data storage locations, each of which holds the same type of data. Think of an array series of subdivisions, called elements, to store the data.
7, 6, 5, 8, 3, 9, 2, 6, 10, 2
While this array contains only 10 elements, It is important that all of the elements in an array must contain the same type of data
Suppose you must calculate the average of 3 values:
score1 = Console.readInt("Enter score one" );
score2 = Console.readInt("Enter score two");
score3 = Console.readInt("Enter score three");
average = ( score1 + score2 + score3 ) / 3;
If you have to deal with a large number of scores, you could use a loop.
sum = 0;
for ( i = 1; i <= 50; i++ )
{
score = Console.readInt("Enter score");
sum = sum + score;
}
average = sum / 50.0;
With an Array
dealing with large amounts of data can present potential problems, With an array, however, only one variable name is needed and all entries will remain in the memory for future use. not 50 separate names) with 50 cells to hold the entries.
All entries will remain in the memory for future use.
The name of the array is the address of the first element of the array!