Today, you learn about three of the most boring features in the Java language:
How to make part of a Java program repeat itself by using loops
How to make a program decide whether to do something based on logic
How to organize groups of the same class or data type into lists called arrays
If these features don’t sound boring to you, they shouldn’t. Most of the significant work that you will accomplish with your Java software will use all three.
These topics are boring for computers because they enable software to do one of the things at which it excels: performing repetitive tasks over and over.
At this point, you have dealt with only a few variables in each Java program. In some cases, it’s manageable to use individual variables to store information, but what if you had 20 items of related information to track? You could create 20 different variables and set up their initial values, but that approach becomes progressively more cumbersome as you deal with larger amounts of information. What if there were 100 items, or even 1,000?
Arrays are a way to store a list of items that have the same primitive data type, the same class, or a common parent class. Each item on the list goes into its own numbered slot so that you can easily access the information.
Arrays can contain any type of information that is stored in a variable, but after the array is created, you can use it for that information type only. For example, you can have an array of integers, an array of String
objects, or an array of arrays, but you can’t have an array that contains both String
objects and integers.
Java implements arrays differently than some other languages do—as objects treated like other objects.
To create an array in Java, you must do the following:
Declare a variable to hold the array.
Create a new array object and assign it to the array variable.
Store information in that array.
The first step in array creation is to declare a variable that will hold the array. Array variables indicate the object or data type that the array will hold and the name of the array. To differentiate from regular variable declarations, a pair of empty brackets ([]
) is added to the object or data type, or to the variable name.
The following statements are examples of array variable declarations:
String[] requests; Point[] targets; float[] donations;
You also can declare an array by putting the brackets after the variable name instead of the information type, as in the following statements:
String requests[]; Point targets[]; float donations[];
After you declare the array variable, the next step is to create an array object and assign it to that variable. To do this:
Use the new
operator.
Initialize the contents of the array directly.
Because arrays are objects in Java, you can use the new
operator to create a new instance of an array, as in the following statement:
String[] players = new String[10];
This statement creates a new array of strings with 10 slots that can contain String
objects. When you create an array object by using new
, you must indicate how many slots the array will hold. This statement does not put actual String
objects in the slots; you must do that later.
Array objects can contain primitive types, such as integers or Booleans, just as they can contain objects:
int[] temps = new int[99];
When you create an array object using new
, all its slots automatically are given an initial value (0
for numeric arrays, false
for Booleans, ‘