Arrays
In non-trivial computing problems you often need to store lists of items. Often these items can be specified sequentially and referred to by their position in the list. Sometimes this ordering is natural as in a list of the first ten people to arrive at a sale. The first person would be item one in the list, the second person to arrive would be item two, and so on. Other times the ordering doesn’t really mean anything such as in the ram configuration problem of the previous chapter where having a 4 MB SIMM in slot A and an 8 MB SIMM in slot B was effectively the same as an 8 MB SIMM in slot A and a 4 MB SIMM in slot B. However it’s still convenient to be able to assign each item a unique number and enumerate all the items in a list by counting out the numbers.
There are many ways to store lists of items including linked lists, sets, hashtables, binary trees and arrays. Which one you choose depends on the requirements of your application and the nature of your data. Java provides classes for many of these ways to store data and we’ll explore them in detail in the chapter on the Java Class Library.
Arrays are probably the oldest and still the most generally effective means of storing groups of variables. An array is a group of variables that share the same name and are ordered sequentially from zero to one less than the number of variables in the array. The number of variables that can be stored in an array is called the array’s dimension. Each variable in the array is called an element of the array.
An array can be visualized as a column of data like so:
|
|
In this case we’re showing an integer array named I
with five elements; i.e. the type of the array is int
and the array has dimension 5.
In non-trivial computing problems you often need to store lists of items. Often these items can be specified sequentially and referred to by their position in the list. Sometimes this ordering is natural as in a list of the first ten people to arrive at a sale. The first person would be item one in the list, the second person to arrive would be item two, and so on. Other times the ordering doesn’t really mean anything such as in the ram configuration problem of the previous chapter where having a 4 MB SIMM in slot A and an 8 MB SIMM in slot B was effectively the same as an 8 MB SIMM in slot A and a 4 MB SIMM in slot B. However it’s still convenient to be able to assign each item a unique number and enumerate all the items in a list by counting out the numbers.
There are many ways to store lists of items including linked lists, sets, hashtables, binary trees and arrays. Which one you choose depends on the requirements of your application and the nature of your data. Java provides classes for many of these ways to store data and we’ll explore them in detail in the chapter on the Java Class Library.
Arrays are probably the oldest and still the most generally effective means of storing groups of variables. An array is a group of variables that share the same name and are ordered sequentially from zero to one less than the number of variables in the array. The number of variables that can be stored in an array is called the array’s dimension. Each variable in the array is called an element of the array.
An array can be visualized as a column of data like so:
I[0]
I[1]
I[2]
I[3]
I[4]
4
2
76
-90
6
In this case we’re showing an integer array named I with five elements; i.e. the type of the array is int and the array has dimension 5.