Activity 16.7.1.
Run the following code. Try adding more elements to the ArrayList. How would you get out the last element of the ArrayList?
java.util.*
, contains a good number of classes and interfaces designed to facilitate storing and manipulating groups of objects. This family of related interfaces and classes is called the Java collections framework. It contains structures that correspond to the ADTs that we have just discussed, plus other data structures. Java 5.0 has reimplemented the Java collections framework using generic types that allow a programmer to specify a type for the objects that are stored in the structure.<E>
or <K,V>
to refer to unspecified type names. Thus, names of classes or interfaces implemented with generic types are written with the syntax ClassName<E>
.ArrayList
class, which was introduced in Chapter 9. The ArrayList<E>
class, which is part of the Java collections framework, has a generic type implementation in Java 5.0. The ArrayList
class contains the methods listed in Figure 16.7.1 for storing and retrieving objects, and for accessing objects by their index position within the ArrayList
.E
refers to an unspecified type name, that is, the name of a class or interface. This type is specified when a corresponding variable is declared. The type must also be included after a constructor’s type name when an object is instantiated and assigned to the variable. The following code demonstrates how to create a ArrayList<E>
object for storing String
objects.<E>
serves as parameter for the type of objects that will be stored in the ArrayList
. One benefit that a generic type provides is type checking of method arguments at compile time. If strList
is a ArrayList<String>
object, then the statement strList.add(new Integer(57));
will generate a compile-time error. By contrast, if strList
was just a plain ArrayList
object, no error would be found at compile time. Thus, if a programmer wishes to create an array of String
objects, using generic types will help guarantee that the objects being stored are actually of type String
. In this way, using generic types helps to reduce the number of programming errors and thereby makes programs safer and more robust.java.util.Stack<E>
class
Stack<E>
class, implemented as a subclass of the Vector<E>
class which has been replaced by the ArrayList<E>
class. It contains the methods shown in Figure 16.7.2. For the most part, its methods provide the same functionality as the methods we developed earlier in this chapter.ArrayList<E>
code.java.util.Stack<E>
class instead of the Stack
class we defined in the earlier sections.List<E>
interface and the LinkedList<E>
class
java.util.LinkedList<E>
is an implementation of a linked list (Figure 16.7.3). Like our implementation earlier in this chapter, it contains methods that can be used to define the standard stack and queue methods.java.util.List<E>
interface. The advantage of defining list operations as an interface is that they can be implemented by a number of data structures. Code for using the list methods can be written to work independently of the data structure being used.LinkedList<E>
and ArrayList<E>
, both of which implement the List<E>
interface. In this section, we will demonstrate how to make appropriate use of the List<E>
interface and data structures that implement it.LinkedList<E>
data structure to store objects of the PhoneRecord
class that was defined earlier in this chapter and will use methods of the List<E>
interface to manipulate the data. A list seems to be an appropriate structure for this problem sinceList<E>
and LinkedList<E>
structures will be used.List<PhoneRecord> theList = new LinkedList<PhoneRecord>();
theList
of interface type List<E>
but assigns an object of class type LinkedList<E>
. This is appropriate because the class implements the interface and the code uses only methods from the interface. The class ArrayList<E>
in the collections framework also implements the List<E>
interface. It uses an array rather than a linked list to store elements and has a constructor with an int
parameter that sets the size of the array. If the programmer knew that theList
would contain close to, but always less than, 100 elements, then it might be better to declare:List<PhoneRecord> theList = new ArrayList<PhoneRecord>(100);
for
loop at the end of the method. This is a new feature of Java 5.0 which can be used to simplify the coding of loops that iterate through every object in a collection of objects. The statementfor (PhoneRecord pr : theList) { *** }
PhoneRecord
object, pr
, in the theList
data structure. In previous versions of Java, an interface named Iterator
had to be used to enumerate all the elements in a collection. The Iterator
approach is more flexible—for example, it allows you to iterate through just some of the members of the collection— and will therefore still have to be used for more complex loops.Roger M 090-997-2918
Jane M 090-997-1987
Stacy K 090-997-9188
Gary G 201-119-8765
Jane M 090-997-1987
Set
interface and the Map
interface.