[ Pobierz całość w formacie PDF ]

method to get a ListIterator:
public Iterator iterator()
public ListIterator listIterator()
public ListIterator listIterator(int index)
Since the elements of a list are ordered, calling iterator() or listIterator() with no arguments returns the same
collection of ordered elements. The only difference is the set of methods you can use with the returned
iterator. The second version of listIterator() allows you to get an iterator starting with any position within the
list.
Using an iterator returned from a List is like using any other iterator. The only difference is that the order of
the elements in the list is preserved:
List list = Arrays.asList(new String[] {"Hit List", "To Do List", "Price List",
"Top Ten List"});
Iterator iter = list.iterator();
while (iter.hasNext()) {
System.out.println(iter.next());
}
Running this will result in the following output:
Hit List
To Do List
Price List
Top Ten List
ListIterator will be discussed in further detail later in this chapter.
Finding Elements
Besides fetching elements of the list, you can check to see if the list contains a single element or collection of
elements.
115
Finding Elements
Checking for Existence
Use the contains() method to check if a specific element is part of the list:
public boolean contains(Object element)
If found, contains() returns true, if not, false. As with remove(), equality checking is done through the equals()
method of the element.
Checking for Position
If, instead of seeing only whether an element is in the list, you want to know where in the list it's located,
that's where the indexOf() and lastIndexOf() methods come in:
public int indexOf(Object element)
public int lastIndexOf(Object element)
Starting at the beginning or end, you can find out where in the vector the next instance of a specific element is
located. Unlike Vector, there is no way to start at a position other than the beginning or end.
Both indexOf() and lastIndexOf() use equals() to check for equality and return  if the element is not found.
Also, the found position is reported from the beginning of the list, not relative to the position searched from.
If you're interested in finding all of the positions for a single element in an ArrayList, you're out of luck.
You'll need to convert the list to a Vector and use the versions of indexOf() or lastIndexOf() that support a
starting position other than the end.
Checking for List Containment
In addition to checking whether the list includes a specific element, you can check to see if the list contains a
whole collection of other elements with the containsAll() method:
public boolean containsAll(Collection c)
This method takes a Collection as its argument and reports if the elements of the passed-in collection are a
subset of the current list. In other words, is each element of the collection also an element of the list? The
current list can contain other elements but the passed-in collection cannot or containsAll() will return false. If
an element is in the passed-in collection multiple times, it only needs to be in the source list once to be
successful.
Replacing Elements
You can replace elements in a list with the set() method:
public Object set(int index, Object element)
Use the set() method when you need to replace the element at a specific position in the list. The object being
replaced is returned by the set() method. If the index happens to be invalid, an IndexOutOfBoundsException
is thrown.
116
Checking Size
Checking Size
To find out how many elements are in a list, use its size() method:
public int size()
ArrayList also has an isEmpty() method to get the size and to check to see if no elements are in the list:
public boolean isEmpty()
Checking Capacity
Similar to a Vector, an ArrayList has a capacity as well as a size. The capacity represents the size of the
internal backing store, or the number of elements the array list can hold before the internal data structure
needs to be resized. Minimizing excess memory usage by the backing store preserves memory but causes a
higher insertion-time price when that capacity is exceeded. Working with capacity properly can improve
performance immensely without wasting too much memory. [ Pobierz całość w formacie PDF ]

  • zanotowane.pl
  • doc.pisz.pl
  • pdf.pisz.pl
  • lastella.htw.pl