Array Read and Write
The two most primitive Array operations are writing elements into them, and reading elements from them. All other Array operations are built on top of these two primitive operations.
Writing Elements into an Array
When you create an Array, you specify the number of elements it can hold. This is called the Array's capacity. The capacity is fixed, and cannot be changed. Once you create an Array, you can only write elements into it, but you cannot change the capacity. If you want to write more elements into the Array, you'll need to create a new Array with a larger capacity.
To put a PokemonCard into the Array, we need to decide which of the 15 places in the Array we want to put it. We call this the index. The index is a number that tells us which place in the Array we want to write to. The index for 15
is always a number between 0
and 14
(the number of places in the Array minus one and starts with 0
).
To write a PokemonCard into the Array, we use the following code:
// The actual code for writing a PokemonCard into the Array.
PokemonCard pikachu = new PokemonCard("Pikachu");
myCollection[11] = pikachu; // this will write the Pikachu PokemonCard into the 12th place in the Array.
// A simple definition for a PokemonCard.
public class PokemonCard {
public String name;
public PokemonCard(String name) {
this.name = name;
}
}
The code above will write the Pikachu PokemonCard into the 12th place in the Array. The index for the 12th place is 11
(the number of places in the Array minus one and starts with 0
).
If you try to write to an index that is outside the range of the Array, you'll get an error. For example, if you try to write to the 16th
place in the Array, you'll get an error because the Array only has 15
places.
Writing With a Loop
We commonly use a loop to put lots of values into an Array. To illustrate this, let's go to another example. This time, we're going to create an Array of int
and put the first 10
square numbers into it.
// The actual code for writing items into an Array with a loop.
int[] squares = new int[10];
for (int i = 0; i < squares.length; i++) {
squares[i] = i * i;
}
The code above will write the first 10
square numbers into the Array.
Reading Elements from an Array
To read a PokemonCard from the Array, we need to decide which of the 15 places in the Array we want to read from. We call this the index. The index is a number that tells us which place in the Array we want to read from. The index for array of length 15
is always a number between 0
and 14
(the number of places in the Array minus one and starts with 0
).
To read a PokemonCard from the Array, we use the following code:
// The actual code for reading a PokemonCard from the Array.
System.out.println(myCollection[11].name); // "Pikachu"
System.out.println(myCollection[12].name); // null
The code above will read the 12th place in the Array and print the name of the PokemonCard in the 12th place in the Array. The index for the 12th place is 11
(the number of places in the Array minus one and starts with 0
).
Notice that because we haven't yet put anything at index 12
, the value it contains is null
. In other languages, such as C
, the Array slot could contain completely random data. Java
always initializes empty Array slots to null
if the Array contains objects, or to default values if it contains primitive types. For example, the array int []
would contain the default value of 0
for each element, float[]
would contain default values of 0.0
, and bool[]
would contain default values of false
.
Reading With a Loop
We commonly use a loop to read lots of values from an Array. To illustrate this, let's go to another example. This time, we're going to read the first 10
square numbers from the Array.
// The actual code for reading items from an Array with a loop.
int[] squares = new int[10];
for (int i = 0; i < squares.length; i++) {
squares[i] = i * i;
}
for (int i = 0; i < squares.length; i++) {
System.out.println(squares[i]);
}
// Output:
// 0
// 1
// 4
// 9
// 16
// 25
// 36
// 49
// 64
// 81
The code above will read the first 10
square numbers from the Array.