A list is an ordered collection that may include duplicate elements. The user has control over positional placing of elements in a list. The List interface, as well as inheriting all
the Collection methods, includes additional methods that manipulate the position of elements in a list. Three classes in the java.util package implement the List interface: ArrayList, LinkedList, and Vector. ArrayList, in general, is the fastest implementation; if, however, elements are frequently added to the beginning or end of a list or there are frequent deletions from a list, then the LinkedList implementation should be used. Prior to Java version 1.2, Vector was a separate data structure, since version 1.2 Vector is an implementation of the List interface. The Vector class is similar to an
ArrayList; however, Vector is synchronized while ArrayList is unsynchronized. All Collection framework implementations, apart from Vector and Hashtable, are unsynchronized.
Consider CollectExample instead of adding elements to a set, we will add them to a list using the ArrayList implementation.
CollectExample
import java.util.*;
public class CollectExample
{
public static void main(String[] args)
{
Collection c = new ArrayList();
c.add(''Smith");
c.add("Jones");
c.add("Smith");
c.add(''Brown");
c.add("Able");
Iterator i = c.iterator();
while (i.hasNext() ) {
System.out.print(i.next() + " ");
}
}
}
The program will now output:
java CollectExample
Smith Jones Smith Brown Able
Note that since we have a list, the duplicate elements ''Smith" are permitted. This example illustrates the polymorphic behavior of Collections. Our Collection interface methods work regardless of whether the implementation is a Set or List.
If we want to use the LinkedList or Vector implementations in the program we only need to change the one statement. Both ArrayList and Vector have a default initial capacity of ten elements. This is automatically incremented when the list becomes full. We can change these default tuning parameters with the constructors ArrayList(int capacity) and Vector(int capacity). There are no tuning parameters for the LinkedList constructor.
There are a number of methods in the List interface such as set, get, and subList that are not inherited from the Collection interface. To use any of these methods, we replace
the Collection type in line 6 with a List type, as follows:
List c = new ArrayList();
The set method is used to replace an element in a specified position in a list with a new element. For example, the statement
c.set(3, "BROWN");
replaces the fourth element (count from zero) "Brown" with "BROWN".
The get method returns the element at the specified position. For example,
Object o = c.get(3);
returns the fourth element of c.
The subList(startposition, endposition) method is used to create a sublist from the current list starting at startposition inclusive to endposition exclusive. For example, the statement
List c1 = c.subList(2, 4);
creates a sublist c1 with contents ''Smith", "Brown".
The List interface also provides its own iterator, ListIterator, in addition to the Collection interface iterator. ListIterator inherits all the methods of Iterator but in addition provides methods such as previous and hasPrevious for iterating through a list in the reverse direction. previous returns the previous element in the list. hasPrevious returns true if there are more elements in the list when iterating in the reverse direction.
The statement
ListIterator l = c.listIterator(5);
creates a ListIterator, l, starting at the fourth (last) element in the list c. The argument, 5, is the element that would be retrieved by a call to the next method. The following code fragment iterates backward through our list:
while (l.hasPrevious() )
{
System.out.print(l.previous() + " ");
}
producing the result
Able Brown Smith Jones Smith
the Collection methods, includes additional methods that manipulate the position of elements in a list. Three classes in the java.util package implement the List interface: ArrayList, LinkedList, and Vector. ArrayList, in general, is the fastest implementation; if, however, elements are frequently added to the beginning or end of a list or there are frequent deletions from a list, then the LinkedList implementation should be used. Prior to Java version 1.2, Vector was a separate data structure, since version 1.2 Vector is an implementation of the List interface. The Vector class is similar to an
ArrayList; however, Vector is synchronized while ArrayList is unsynchronized. All Collection framework implementations, apart from Vector and Hashtable, are unsynchronized.
Consider CollectExample instead of adding elements to a set, we will add them to a list using the ArrayList implementation.
CollectExample
import java.util.*;
public class CollectExample
{
public static void main(String[] args)
{
Collection c = new ArrayList();
c.add(''Smith");
c.add("Jones");
c.add("Smith");
c.add(''Brown");
c.add("Able");
Iterator i = c.iterator();
while (i.hasNext() ) {
System.out.print(i.next() + " ");
}
}
}
The program will now output:
java CollectExample
Smith Jones Smith Brown Able
Note that since we have a list, the duplicate elements ''Smith" are permitted. This example illustrates the polymorphic behavior of Collections. Our Collection interface methods work regardless of whether the implementation is a Set or List.
If we want to use the LinkedList or Vector implementations in the program we only need to change the one statement. Both ArrayList and Vector have a default initial capacity of ten elements. This is automatically incremented when the list becomes full. We can change these default tuning parameters with the constructors ArrayList(int capacity) and Vector(int capacity). There are no tuning parameters for the LinkedList constructor.
There are a number of methods in the List interface such as set, get, and subList that are not inherited from the Collection interface. To use any of these methods, we replace
the Collection type in line 6 with a List type, as follows:
List c = new ArrayList();
The set method is used to replace an element in a specified position in a list with a new element. For example, the statement
c.set(3, "BROWN");
replaces the fourth element (count from zero) "Brown" with "BROWN".
The get method returns the element at the specified position. For example,
Object o = c.get(3);
returns the fourth element of c.
The subList(startposition, endposition) method is used to create a sublist from the current list starting at startposition inclusive to endposition exclusive. For example, the statement
List c1 = c.subList(2, 4);
creates a sublist c1 with contents ''Smith", "Brown".
The List interface also provides its own iterator, ListIterator, in addition to the Collection interface iterator. ListIterator inherits all the methods of Iterator but in addition provides methods such as previous and hasPrevious for iterating through a list in the reverse direction. previous returns the previous element in the list. hasPrevious returns true if there are more elements in the list when iterating in the reverse direction.
The statement
ListIterator l = c.listIterator(5);
creates a ListIterator, l, starting at the fourth (last) element in the list c. The argument, 5, is the element that would be retrieved by a call to the next method. The following code fragment iterates backward through our list:
while (l.hasPrevious() )
{
System.out.print(l.previous() + " ");
}
producing the result
Able Brown Smith Jones Smith
No comments:
Post a Comment