E
- Entry typepublic class Vector<E> extends Object implements Cloneable
Vector
class implements a growable array of
objects. Like an array, it contains components that can be
accessed using an integer index. However, the size of a
Vector
can grow or shrink as needed to accommodate
adding and removing items after the Vector
has been created.Modifier and Type | Field and Description |
---|---|
protected int |
capacityIncrement
When the
Vector needs to grow its size is increased by this
amount. |
protected int |
elementCount
The number of elements stored.
|
protected Object[] |
elementData
Array buffer into which elements are stored.
|
Constructor and Description |
---|
Vector()
Constructs an empty vector with the specified initial capacity set
to 6 and with its capacity increment equal to zero.
|
Vector(int initcap)
Constructs an empty vector with the specified initial capacity and
with its capacity increment equal to zero.
|
Vector(int initcap,
int capinc)
Constructs an empty vector with the specified initial capacity and
capacity increment.
|
Modifier and Type | Method and Description |
---|---|
void |
addElement(E obj)
Adds an element to the end of the vector increasing its size
by 1.
|
int |
capacity()
Returns the capacity of the vector (not the count of elements
that are stored within it).
|
Object |
clone()
Returns a clone of this vector.
|
boolean |
contains(E obj)
Returns
true if this vector contains the specified element. |
void |
copyInto(Object[] aobj)
Copies the components of this vector into the specified array.
|
E |
elementAt(int index)
Returns the element at the specified index.
|
Enumeration<E> |
elements()
Returns an enumeration of the components of this vector.
|
void |
ensureCapacity(int mincap)
Increases the capacity of this vector, if necessary, to ensure
that it can hold at least the number of components specified by
the minimum capacity argument.
|
E |
firstElement()
Returns the first component (the item at index
0 ) of
this vector. |
int |
indexOf(Object obj)
Returns the index of the first occurrence of the specified element
in this vector, or -1 if this vector does not contain the element.
|
int |
indexOf(Object obj,
int index)
Returns the index of the first occurrence of the specified element in
this vector, searching forwards from
index , or returns -1 if
the element is not found. |
void |
insertElementAt(E obj,
int index)
Inserts the specified object as a component in this vector at the
specified
index . |
boolean |
isEmpty()
Tests if this vector has no elements.
|
E |
lastElement()
Returns the last element in the vector.
|
int |
lastIndexOf(Object obj)
Returns the index of the last occurrence of the specified element
in this vector, or -1 if this vector does not contain the element.
|
int |
lastIndexOf(Object obj,
int index)
Returns the index of the last occurrence of the specified element in
this vector, searching backwards from
index , or returns -1 if
the element is not found. |
void |
removeAllElements()
Removes all elements from this vector and sets its size to zero.
|
boolean |
removeElement(Object obj)
Removes the first (lowest-indexed) occurrence of the argument
from this vector.
|
void |
removeElementAt(int index)
Deletes the component at the specified index.
|
void |
setElementAt(E obj,
int index)
Sets the element at the specified
index of this
vector to be the specified object. |
void |
setSize(int size)
Sets the size of this vector.
|
int |
size()
Returns the number of components in this vector.
|
String |
toString()
Returns a string representation of this Vector, containing
the String representation of each element.
|
void |
trimToSize()
Trims the capacity of this vector to be the vector's current
size.
|
protected Object[] elementData
protected int elementCount
protected int capacityIncrement
Vector
needs to grow its size is increased by this
amount.public Vector()
public Vector(int initcap)
initcap
- initial capacitypublic Vector(int initcap, int capinc)
initcap
- initial capacity?capinc
- capacity incrementpublic void addElement(E obj)
obj
- an object to be addedpublic int capacity()
public Object clone()
Vector
object.public boolean contains(E obj)
true
if this vector contains the specified element.obj
- element whose presence is to be testedtrue
if this contains the elementpublic void copyInto(Object[] aobj)
aobj
- the array receiving the elementspublic E elementAt(int index)
index
- an index into this vectorpublic Enumeration<E> elements()
public void ensureCapacity(int mincap)
mincap
- the desired minimum capacitypublic E firstElement()
0
) of
this vector.NoSuchElementException
- if this vector has no componentspublic int indexOf(Object obj)
obj
- element to search forpublic int indexOf(Object obj, int index)
index
, or returns -1 if
the element is not found.obj
- element to search forindex
- index to start searching fromindex
or later in the vector;
-1
if the element is not found.public void insertElementAt(E obj, int index)
index
. Each component in this vector with
an index greater or equal to the specified index
is
shifted upward to have an index one greater than the value it had
previously.obj
- the component to insertindex
- where to insert the new componentArrayIndexOutOfBoundsException
- if the index is out of range
(index < 0 || index > size()
)public boolean isEmpty()
true
if and only if this vector has
no elements, that is, its size is zero;
false
otherwise.public E lastElement()
NoSuchElementException
- if this vector is emptypublic int lastIndexOf(Object obj)
obj
- element to search forpublic int lastIndexOf(Object obj, int index)
index
, or returns -1 if
the element is not found.obj
- element to search forindex
- index to start searching backwards fromindex
in this vector;
-1 if the element is not found.public void removeAllElements()
public boolean removeElement(Object obj)
obj
- the component to be removedtrue
if the argument was a component of this
vector; false
otherwise.public void removeElementAt(int index)
index
is shifted downward to have an index one
smaller than the value it had previously. The size of this vector
is decreased by 1
.index
- the index of the object to removeArrayIndexOutOfBoundsException
- if the index is out of range
(index < 0 || index >= size()
)public void setElementAt(E obj, int index)
index
of this
vector to be the specified object. The previous component at that
position is discarded.obj
- what the component is to be set toindex
- the specified indexArrayIndexOutOfBoundsException
- if the index is out of range
(index < 0 || index >= size()
)public void setSize(int size)
null
items are added to the end of
the vector. If the new size is less than the current size, all
components at index newSize
and greater are discarded.size
- the new size of this vectorpublic int size()
public String toString()
public void trimToSize()
elementData
,
with a smaller one. An application can use this operation to
minimize the storage of a vector.