Sub topic 2
Declaring, Creating, and Initializing Arrays
An array is a fixed-size sequence of values of the same type. Each value in the sequence is called an element. Arrays are useful when related values, such as test scores, belong together.
To use an array, you declare a variable, create the array, and give its elements values. Java lets you combine some of these steps.
Declare an array variable
A declaration tells Java the type and name of a variable:
int[] scores;
The type int holds whole numbers. The brackets [] mean that scores can refer to an array of whole numbers.
A reference is a way to refer to an object, such as an array. This declaration does not create an array yet. It only declares a variable that can refer to one.
Create an array
The new keyword creates an array:
scores = new int[3];
The number inside the brackets is the array’s size: this array has three elements. Its size cannot change after it is created.
Java gives the elements starting values when you create an array this way. For an int array, each starting value is 0. You can change element values later.
You can also declare the variable and create the array in one statement:
int[] scores = new int[3];
Initialize with values
Initialization means giving something its starting value. An array initializer lists the starting element values between braces:
int[] scores = {80, 90, 100};
Java creates an array with three elements because the list contains three values. You do not need to write the size separately.
This shorter form works when you declare the variable. If you declare it first and assign the array later, include new int[]:
int[] scores;
scores = new int[] {80, 90, 100};
In this form, Java counts the listed values to set the size. Do not put a size between the brackets as well.
Try it
Save this program as ArraySetup.java:
public class ArraySetup {
public static void main(String[] args) {
int[] scores;
scores = new int[3];
int[] readyScores = {80, 90, 100};
int[] laterScores;
laterScores = new int[] {70, 85};
String[] names = {"Ada", "Lin"};
int[] empty = new int[0];
System.out.println(java.util.Arrays.toString(scores));
System.out.println(java.util.Arrays.toString(readyScores));
System.out.println(java.util.Arrays.toString(laterScores));
System.out.println(java.util.Arrays.toString(names));
System.out.println(java.util.Arrays.toString(empty));
}
}A String holds text. The names array holds String values rather than whole numbers.
The program uses java.util.Arrays.toString to show each array’s contents as text. Compile and run it from the folder containing the file:
javac ArraySetup.java
java ArraySetupThe first line is [0, 0, 0]: Java supplied those starting values. The next three lines show the values written in the initializers. The last line is [], because an array of size zero has no elements.
Common mistakes
- Declaring an array variable does not create an array. Create and assign an array before using it.
- The size in
new int[3]is a count, not a list of values. Use an initializer when you already know the values. - After a separate declaration, use
new int[] {1, 2}rather than just{1, 2}. - A variable can later refer to a different array, but an existing array keeps its original size.
Exercise
Declare and create an int array named temperatures with four elements. What are their starting values?
Show answer
Write int[] temperatures = new int[4];. All four starting values are 0.
Exercise
After writing int[] values;, how can you assign it an array containing 5 and 10?
Show answer
Write values = new int[] {5, 10};. Java creates an array with two elements.