Collections

A

Describe the fundamental differences between arrays, lists, and maps. Further describe when you would use an array, a list, or a map in your program. Are you using these various data structures in our project?

B

Create a class Gerbil that has a private variable called number of type integer. Add a public method jump() that print outs the gerbil's number. Create an ArrayList of 42 Gerbils. 1) Create a method that uses the "for-each" loop control structure to run through the list of Gerbil objects and make the gerbils jump. 2) Create a second method that uses the get() method (from ArrayList) to run through the list of Gerbil objects and make the gerbils jump again. 3) Create yet another method that uses an Iterator to run through the list of Gerbils (and make them jump). 4) Create a fourth method that uses a ListIterator to run through the list of gerbils and make them jump.

C

Modify Exercise B to use the raw type of ArrayList. Use one of the four methods from Exercise B to run through the list and make the gerbils jump. Note you are forced to use explicit downcasts. Are the compiler providing you with warnings?

D

Modify Exercise B but instead of using the ArrayList class use the LinkedList class. How many places do you have to change your code to implement this change (you still have run through the list in the four different ways described in Exercise B)? Explain why you (hopefully) only have to make very few changes to your code to switch from using the ArrayList class to the LinkedList class. Modify Exercise B again and instead of the ArrayList class use the HashSet class. How many of the iteration approaches from Exercise B can you use?

E

1) Create a class that contains a TreeMap that map the ASCII characters to their ASCII integer values, e.g., 'A' => 65, 'B' => 66, 'C' = 67 etc. Iterate over the TreeMap and retrieve all the Map.Entry pairs and print them to screen. 2) Try to insert a duplicate into the TreeMap. Are any exceptions thrown?