The use of integer index values to access objects in a collection is something that we will see over and over again—not just with ArrayLists but also with several different types of collections. So it is important to understand what we have seen of this so far: that the index values start at zero; that the objects are numbered sequentially; and that there are usually no gaps in the index values of consecutive objects in the collection. Using integer values as indices also makes it very easy to express in program code expressions such as “the next item” and “the previous item” with respect to an item in the collection. If an item is at index p, then “the next” one will be at index (p+1) and “the previous” one is now at index (p–1). We can also map natural-language selections such as “the first three” to program-related terminology. For example, “the items at indices 0, 1, and 2” or “the last four” could be “the items at indices (list.size()-4) to (list.size()-1)”. We could even imagine working our way through the entire collection by having an integer index variable whose value is initially set to zero and is then successively increased by 1, passing its value to the get method to access each item in the list in order (stopping when it goes beyond the final index value of the list). But we are getting a little ahead of ourselves. Nevertheless, in a little while we will see how all this works out in practice when we look at loops and iteration. Write an alternative version of checkIndex called validIndex. It takes an integer parameter and returns a boolean result. It does not print anything, but returns true if the parameter’s value is a valid index for the current state of the collection and false otherwise. Test your method on the object bench with both valid and invalid parameters. Test the empty case too.
The general utility of numbering with collections
The use of integer index values to access objects in a collection is something that we will see over and over again—not just with ArrayLists but also with several different types of collections. So it is important to understand what we have seen of this so far: that the index values start at zero; that the objects are numbered sequentially; and that there are usually no gaps in the index values of consecutive objects in the collection.
Using integer values as indices also makes it very easy to express in program code expressions such as “the next item” and “the previous item” with respect to an item in the collection. If an item is at index p, then “the next” one will be at index (p+1) and “the previous” one is now at index (p–1). We can also map natural-language selections such as “the first three” to program-related terminology. For example, “the items at indices 0, 1, and 2” or “the last four” could be “the items at indices (list.size()-4) to (list.size()-1)”. We could even imagine working our way through the entire collection by having an integer index variable whose value is initially set to zero and is then successively increased by 1, passing its value to the get method to access each item in the list in order (stopping when it goes beyond the final index value of the list).
But we are getting a little ahead of ourselves. Nevertheless, in a little while we will see how all this works out in practice when we look at loops and iteration.
Write an alternative version of checkIndex called validIndex. It takes an integer parameter and returns a boolean result. It does not print anything, but returns true if
the parameter’s value is a valid index for the current state of the collection and false otherwise. Test your method on the object bench with both valid and invalid parameters. Test the empty case too.
Step by step
Solved in 2 steps