Java Reference
In-Depth Information
Exercises
1. Modify the Sieve program developed in Section 11.1 to make two optimizations. First, instead of storing all integers
up to the maximum in the numbers list, store only 2 and all odd numbers from 3 upward. Second, write code to
ensure that if the first number in the numbers list ever reaches the square root of the maximum, all remaining values
from the numbers list are moved into the primes list. (Why is this a valid operation?)
2. Write a method called alternate that accepts two List s as its parameters and returns a new List containing alter-
nating elements from the two lists, in the following order:
• First element from first list
• First element from second list
• Second element from first list
• Second element from second list
• Third element from first list
• Third element from second list
•. . .
If the lists do not contain the same number of elements, the remaining elements from the longer list should be placed
consecutively at the end. For example, for a first list of (1, 2, 3, 4, 5) and a second list of (6, 7, 8, 9,
10, 11, 12) , a call of alternate(list1, list2) should return a list containing (1, 6, 2, 7, 3, 8, 4, 9,
5, 10, 11, 12) .
3. Write a method called removeInRange that accepts four parameters: a LinkedList , an element value, a starting
index, and an ending index. The method's behavior is to remove all occurrences of the given element that appear in
the list between the starting index (inclusive) and the ending index (exclusive). Other values and occurrences of the
given value that appear outside the given index range are not affected.
For example, for the list (0, 0, 2, 0, 4, 0, 6, 0, 8, 0, 10, 0, 12, 0, 14, 0, 16) , a call of
removeInRange(list, 0, 5, 13) should produce the list (0, 0, 2, 0, 4, 6, 8, 10, 12, 0, 14, 0, 16) .
Notice that the zeros located at indexes between 5 inclusive and 13 exclusive in the original list (before any modifi-
cations were made) have been removed.
4. Write a method called partition that accepts a list of integers and an integer value E as its parameter, and rearranges
(partitions) the list so that all the elements with values less than E occur before all elements with values greater than E .
The exact order of the elements is unimportant, so long as all elements less than E appear before all elements greater than
E . For example, for the linked list (15, 1, 6, 12, -3, 4, 8, 21, 2, 30, -1, 9) , one acceptable ordering of
the list after a call of partition(list, 5) would be (-1, 1, 2, 4, -3, 12, 8, 21, 6, 30, 15, 9) . You
may assume that the list contains no duplicates and does not contain the element value E .
5. Write a method called sortAndRemoveDuplicates that accepts a list of integers as its parameter and rearranges
the list's elements into sorted ascending order, as well as removing all duplicate values from the list. For example,
the list (7, 4, -9, 4, 15, 8, 27, 7, 11, -5, 32, -9, -9) would become (-9, -5, 4, 7, 8, 11,
15, 27, 32) after a call to your method. Use a Set as part of your solution.
6. Write a method countUnique that accepts a list of integers as a parameter and returns the number of unique integer
values in the list. Use a set as auxiliary storage to help you solve this problem. For example, if a list contains the values
(3, 7, 3, -1, 2, 3, 7, 2, 15, 15) , your method should return 5 . The empty list contains 0 unique values.
Search WWH ::




Custom Search