Java Reference
In-Depth Information
public int
indexOf(Object obj, int index)
//Starting at index, the method returns the position of the
//first occurrence of the element specified by obj in the vector.
//If item is not in the vector, the method returns -1.
public boolean
isEmpty()
//Returns true if the vector is empty; otherwise it returns false
public void
removeAllElements()
//Removes all elements of the vector
public void
removeElementAt(
int
index)
//If an element at position specified by index exists, it is
//removed from the vector.
//If index is out of range, this method throws an
//ArrayIndexOutOfBoundsException.
public int
size()
//Returns the number of elements in the vector
public
String toString()
//Returns a string representation of this vector
From Table 9-2, it follows that every element of a
Vector
object is a reference variable of
type
Object
.InJava,
Object
is a predefined class, and a reference variable of the
Object
type can store the address of any object. Because every element of a
Vector
object is a
reference, to add an element to a
Vector
object, you must first create the appropriate object
and store the data into that object. You can then store the address of the object holding the
data into a
Vector
object element. Because every string in Java is considered a
String
object, we will illustrate some of the operations on a
Vector
object using string data.
Consider the following statement:
Vector<String> stringList =
new
Vector<String>();
//Line 1
This statement declares
stringList
to be a reference variable of the
Vector
type, instanti-
ates an empty
Vector
object, and stores the address of this object into
stringList
.The
Vector
object
stringList
is used to create a
Vector
of
String
objects.
9
In Java 5.0 and higher versions, whenever you declare a
Vector
object, you should also
specify the reference type of the objects that the
Vector
object will hold. To do this,
enclose the reference type of the objects between
<
and
>
after the word
Vector
. For
example, in the statement in Line 1,
Vector<String>
specifies that the
Vector
object
stringList
is a
Vector
of the
String
object. If you do not specify the
reference type after the word
Vector
, the compiler will generate a warning message
indicating an unchecked or unsafe operation.
Search WWH ::
Custom Search