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.

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

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.

 

Expert Solution
steps

Step by step

Solved in 2 steps

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY