Java Reference
In-Depth Information
Section 16.4: An IntList Interface
29. What are some advantages of creating an IntList interface and having both types of lists implement it?
30. Write a method called firstLast that can accept either type of integer list as a parameter and that moves the first
element of the list to the end. For example, if a variable called list contains the values [12, 45, 78, 20, 36] , the
call of firstLast(list); will change the list to store [45, 78, 20, 36, 12] .
Section 16.5: LinkedList<E>
31. What are some changes that need to be made to the linked list class to convert it from storing integers to storing
objects of type E ?
32. Why is an iterator especially useful with linked lists?
33. What state does the linked list iterator store? How does the iterator know if there are more elements left to examine?
Exercises
Each of the following exercises is a method to be added to the LinkedIntList class from this chapter.
1. Write a method called set that accepts an index and a value and sets the list's element at that index to have the given
value. You may assume that the index is between 0 (inclusive) and the size of the list (exclusive).
2. Write a method called toString that returns a string representation of the list, such as "[5, -2, 9]" .
3. Write a method called indexOf that accepts a value as a parameter and returns the index in the list of the first occur-
rence of that value, or -1 if the value is not found in the list.
4. Write a method called min that returns the minimum value in a list of integers. If the list is empty, it should throw a
NoSuchElementException .
5. Write a method called isSorted that returns true if the list is in sorted (nondecreasing) order and returns false
otherwise. An empty list is considered to be sorted.
6. Write a method called lastIndexOf that accepts an integer value as a parameter and that returns the index in the
list of the last occurrence of that value, or
1 if the value is not found in the list. For example, if a variable list
stores the values [1, 18, 2, 7, 18, 39, 18, 40] , then the call of list.lastIndexOf(18) should return 6 .
If the call had instead been list.lastIndexOf(3) , the method would return -1 .
7. Write a method called countDuplicates that returns the number of duplicates in a sorted list. The list will be in
sorted order, so all of the duplicates will be grouped together. For example, if a variable list stores the values
[1, 1, 1, 3, 3, 6, 9, 15, 15, 23, 23, 23, 40, 40] , the call of list.countDuplicates() should
return 7 because there are 2 duplicates of 1, 1 duplicate of 3, 1 duplicate of 15, 2 duplicates of 23 and 1 duplicate
of 40.
8. Write a method called hasTwoConsecutive that returns whether or not a list of integers has two adjacent
numbers that are consecutive integers ( true if such a pair exists and false otherwise). For example, if a variable
list stores the values [1, 18, 2, 7, 8, 39, 18, 40] , then the call list.hasTwoConsecutive() should
return true because the list contains the adjacent numbers ( 7, 8 ), which are a pair of consecutive numbers.
 
Search WWH ::




Custom Search