Chapter 14 - Generic Classes.java
Exercise 1: Suppose list is an object of the class ArrayList. How do you add the string "Hello" to the ArrayList list? Exercise 2: Suppose instruction is an object of the class ArrayList that contains the string "Stop" at index position 5. How do you change the string at index position 5 to "Go" (without changing any of the elements at other positions)? Exercise 3: Suppose instruction is an object of the class ArrayList that contains strings at index positions 0 through 10. How do you insert the string "Go" at index position 5 so that no strings are removed from the list instruction? Exercise 4: Can you use the method set to place an element in an ArrayList at any index you want? Exercise 5: Can you use the two-argument version of the method add to add an element in an ArrayList at any index you want? Exercise 6: Consider the following two method invocations. Are there values of index1 that are allowed but that are not allowed for index2? Are there values of index2 that are allowed but that are not allowed for index1? list is an object of the class ArrayList. list.set(index1, "Hello"); list.add(index2, "Hello"); Exercise 7: If you create an ArrayList with the following statement, can the ArrayList contain more than 20 elements?ArrayList myList = new ArrayList(20) Exercise 8: Suppose numberList is an object of the class ArrayList. Give code that will output all the elements in numberList to the screen. Exercise 10: Can you have an ArrayList of int ? Exercise 11: The following for-each loop was used in the method showDifference in Display 14.3. Rewrite it as an ordinary for loop. This should help you to see how much cleaner it is to use a for-each loop. Exercise 12: Define a generic method named getMidindex, which is like getMidpoint, but returns the index of the array midpoint as opposed to the element at the midpoint. Exercise 13: Is an array of type UnorderedPair also of type Pair?
You'll get a 7.5KB .JAVA file.