Data Structures and Java Collection Framework - William Collins - Chapter 4 Solutions

CONCEPT EXERCISES
4.1 What is a collection? What is a collection class? What is a Collection class? Give an example of a collection that is not an instance of a collection class. Programming Project 4.1 has an example of a collection class that is not a Collection class. Get the solution of 4.1


4.2 An array is a collection, even though there is no array class. But an array of objects can be converted into an instance of the ArrayList class. Look in the file Arrays.java in the package java.util to determine the generic algorithm (that is, static method) that converts an array of objects into an ArrayList of those objects. How can that ArrayList then be printed without a loop? Get the solution of 4.2


4.3 .a. Identify each of the following as either an interface or a class:
Collection
LinkedList
Iterator
AbstractList
b. What is the difference between an interface and an abstract class?
c. Of what value is an abstract class? That is, to what extent can an abstract class make a programmer moreproductive? Get the solution of 4.3

4.4 What is a list?  Get the solution of 4.4


PROGRAMMING EXERCISES
4.1 For each of the following, create and initialize a parameterized instance, add two elements to the instance, and then print out the instance:
a. an ArrayList object, scoreList, of Integer objects;
b. a LinkedList object, salaryList, of Double objects;  Get the solution of 4.1


4.2 Develop a main method in which two ArrayList objects are created, one with String elements and one with Integer elements. For each list, add three elements to the list, remove the element at index 1, add an element at index 0, and print out the list. Get the solution of 4.2


4.3 Find an ArrayList method, other than a constructor, that is not also a method in the LinkedList class. Find a LinkedList method, other than a constructor, that is not also a method in the ArrayList class. Get the solution of 4.3


4.4 Suppose we have the following:
LinkedList<String> team = new LinkedList<String> ();
team.add ("Garcia");
Iterator<String> itr = team.iterator();
Integer player = itr.next ();
What error message will be generated? When (at compile-time or at run-time)? Test your hypotheses. Get the solution of 4.4


4.5 Use the ArrayList class three times. First, create an ArrayList object, team1, with elements of type String. Add three elements to team1. Second, create team2, another ArrayList object with elements of type String. Add four elements to team2. Finally, create an ArrayList object, league, whose elements are ArrayList objects in which each element is of type String. Add team1 and team2 to league. Get the solution of 4.5