Storing Information with Java Arrays
See all Sams Teach Yourself on InformIT Programming Tutorials.
No one benefited more from the development of the computer than Santa Claus. For centuries, humankind has put an immense burden on him to gather and process information. Old St. Nick has to keep track of the following things:
Naughty children
Nice children
Gift requests
Homes with impassable chimneys
Women who want more from Santa than Mrs. Claus is willing to let him give
Countries that shoot unidentified aircraft first and ask questions later
Computers were a great boon at the North Pole. They are ideal for the storage, categorization, and study of information.
The most basic way that information is stored in a computer program is by putting it into a variable. However, this method is limited to relatively simple usage. If Santa had to give each naughty child his or her own variable name, he'd be working on the program for the next 12 holiday seasons at least, to say nothing of the effect on his jolly disposition.
The list of naughty children is an example of a collection of similar information. Each child's name is a string of text or some kind of Santa Information System ID number. To keep track of a list of this kind, you can use arrays.
Arrays are groups of related variables that share the same type. You can have arrays of any type of information that can be stored as a variable. Arrays can be used to keep track of more sophisticated types of information than a single variable, but they are almost as easy to create and manipulate as variables.
The following topics will be covered during this hour:
Creating an array
What a dimension of an array is
Giving a value to an array element
Changing the information in an array
Making multidimensional arrays
Sorting an array
Creating Arrays
Arrays are variables that are grouped together under a common name. The term array should be familiar to you, though the meaning might not be so clearthink of a salesman showing off his array of fabulous cleaning products, or a game show with a dazzling array of prizes. Like variables, arrays are created by stating the type of the variable being organized into the array and the name of the array. The difference lies in the addition of the square bracket marks [ and ].
You can create arrays for any type of information that can be stored as a variable. For example, the following statement creates an array of string variables:
String[] naughtyChild;
Here are two more examples:
int[] reindeerWeight; boolean[] hostileAirTravelNations;
NOTE
Java is flexible about where the square brackets are placed when an array is being created. You can put them after the variable name, instead of after the variable type, as in the following:
String niceChild[];
To make arrays easier for humans to spot in your programs, you should probably stick to one style rather than switching back and forth, though Java allows both styles of usage.
The previous examples create arrays, but they do not store any values in them initially. To do this, you must either use the new statement along with the variable type or store values in the array within { and } marks. You also must specify how many different items will be stored in the array. Each item in an array is called an element. The following statement creates an array and sets aside space for the values that it will hold:
int[] elfSeniority = new int[250];
This example creates an array of integers called elfSeniority. The array has 250 elements in it that can be used to store the months that each of Santa's elves has been employed at the Pole. If the rumors are true and Santa runs a union shop, this information is extremely important to keep track of.
When you create an array with the new statement, you must specify the number of elements. Each element of the array is given an initial value when it is set up with new; the value depends on the type of the array. All numeric arrays have the value 0, char arrays have the value '\0', and boolean arrays have the value false. A String array and all other objects are created with the initial value of null.
For arrays that are not extremely large, you can set up their initial values at the same time that you create them. The following example creates an array of strings and gives them initial values:
String[] reindeerNames = { "Dasher", "Dancer", "Prancer", "Vixen", "Comet", "Cupid", "Donder", "Blitzen" };
The information that should be put into elements of the array is placed between { and } brackets, with commas separating each element. The number of elements in the array is not specified in the statement because it is set to the number of elements in the comma-separated list. Each element of the array in the list must be of the same type. The preceding example uses a string for each of the reindeer names.
Once the array is created, you cannot make more space and add another variable to the array. Even if you recall the most famous reindeer of all, you couldn't add "Rudolph" as the ninth element of the reindeerNames array. A Java compiler won't let poor Rudolph join in any reindeerNames.