[ Pobierz całość w formacie PDF ]

where the indexOf() methods come in:
public int indexOf(Object element)
public int indexOf(Object element, int index)
Starting either at the beginning or from some other position, you can find out where in the vector the next
instance of a specific element is located.
The indexOf() method uses the equals() method internally to find out if two elements are the same. The
equals() method will be called to compare the argument with the vector element. If the element is not found,
indexOf() will return  .
Checking for Position from End
The lastIndexOf() method allows you to search in reverse order from the end or some other position, rather
than searching forward from the beginning:
public int lastIndexOf(Object element)
public int lastIndexOf(Object element, int index)
These methods also use equals() (discussed in the preceding section) to check for equality and return  if not
found. While the methods start searching from the end, the index reported is from the beginning. To
demonstrate the use of indexOf() and lastIndexOf(), the following program in Listing 3-1 reports "where's
Waldo" as well as a couple of other names.
Listing 3-1: Finding elements in a vector.
import java.util.Vector;
public class FindVector {
static String members[] =
{"Ralph", "Waldo", "Emerson",
"Henry", "David", "Thoreau",
"Walden", "Pond",
"Thoreau", "Institute"};
public static void main (String args[]) {
Vector v = new Vector();
for (int i=0, n=members.length; i
v.add(members[i]);
}
System.out.println(v);
System.out.println("Contains Society?: " +
v.contains("Society"));
System.out.println("Contains Waldo?: " +
v.contains("Waldo"));
System.out.println("Where's Waldo?: " +
33
Finding Elements
v.indexOf("Waldo"));
System.out.println("Where's Thoreau?: " +
v.indexOf("Thoreau"));
System.out.println("Where's Thoreau from end?: " +
v.lastIndexOf("Thoreau"));
}
}
Running the program in Listing 3-1 generates the following output:
[Ralph, Waldo, Emerson, Henry, David, Thoreau, Walden, Pond, Thoreau, Institute]
Contains Society?: false
Contains Waldo?: true
Where's Waldo?: 1
Where's Thoreau?: 5
Where's Thoreau from end?: 8
Notice that lastIndexOf() reports the position from the beginning, but starts at the end.
If you're interested in finding all the positions for a single element, you'll need to call indexOf() or
lastIndexOf() multiple times. For instance, the following example allows you to separately process each
element contained within the vector that was equal to some value:
Vector v = . . .;
Object value = . . .;
int index = 0;
int length = v.size();
while ((index = 0)) {
index = v.indexOf(value, index);
if (index != -1) {
process(v.get(index));
index++;
}
}
Checking for Collection Containment
The last of the searching methods is the containsAll() method:
public boolean containsAll(Collection c)
This method takes a Collection as its argument, quite possibly (though not necessarily) a Vector, and reports if
the elements of the collection are a subset of the vector's elements. In other words, is each element of the
collection also an element of the vector? The vector can contain other elements but the collection cannot or
containsAll() will return false.
Copying and Cloning Vectors
If you need to make a copy of the elements of a vector, there are several different ways to go about it. You can
either clone the vector, copy the elements into an array, or create a List from a subset of the elements. Which
manner you use depends upon what you plan to do with the results.
34
Copying and Cloning Vectors
Cloning a vector with clone() is like making a shallow copy of the vector, similar to calling the Vector
constructor that accepts a Collection. A new vector is created with each object reference copied from the
original vector. Because new copies of the objects they refer to are not made, all elements of the two vectors
will effectively point to the same set of objects:
public Object clone()
Cloning is useful if you need to reorder a vector's elements while keeping the vector itself intact. For instance,
the following example will clone a vector, then sort the elements of the vector copy.
Vector v1 = . . .;
Vector v2 = (Vector)v1.clone();
Collections.sort(v2); [ Pobierz całość w formacie PDF ]

  • zanotowane.pl
  • doc.pisz.pl
  • pdf.pisz.pl
  • tibiahacks.keep.pl