Java vector usage

Source: Internet
Author: User
Vector class in Java
java.lang.Object   |   +----java.util.Vector
Public class Vector
Extends object
Implements cloneable, serializable
Parent class of the following classes:
Stack

VectorClass implements a dynamically scalable array of objects. Similar to an array, it contains elements that can be accessed through array subscript. HoweverVectorAfter creation.VectorYou can increase or decrease the number of elements to be added or deleted.

Each vector can be maintained throughcapacityAndcapacityIncrementTo optimize the storage space management.capacityAt least as big as the vector size; but it is usually larger, because the vector will add components, the vector storage space will be based oncapacityIncrementAdd. The application increases the vector capacity before inserting a large number of components, which reduces the number of redistribution.


Variable Index
Capacityincrement
When the vector size exceeds its capacity, the vector capacity is automatically increased.
Elementcount
Number of valid elements in the vector.
Elementdata
Stores the Array Buffer of elements in a vector.
Create a sub-index
Vector ()
Construct an empty vector.
Vector (INT)
Construct an empty vector with the specified initial capacity.
Vector (INT, INT)
Construct an empty vector with the specified initial capacity and capacity increment.
Method Index
Addelement (object)
Add a specified component at the end of the vector and add the length to the length.
Capacity ()
Returns the capacity of the current vector.
Clone ()
Returns a clone of the vector.
Contains (object)
Test whether the specified object is an element of the vector.
Copyinto (object [])
Copy the element of the vector to the specified array.
Elementat (INT)
Returns the component at the specified subscript.
Elements ()
Returns an enumeration of the element of the vector.
Ensurecapacity (INT)
Increase vector capacity. If necessary, make sure that it can accommodate at least the number of components specified by the minimum capacity parameter.
Firstelement ()
Returns the first element of the vector.
Indexof (object)
Find the location where a given parameter appears for the first time in the vector, and use equalsMethod to test whether they are equal.
Indexof (object, INT)
Slave indexIn the left-side navigation pane. equalsMethod to test whether they are equal.
Insertelementat (object, INT)
In indexInsert a specified object as the vector element.
Isempty ()
Test whether the vector has no element.
Lastelement ()
Returns the last element of the vector.
Lastindexof (object)
Returns the subscript of the specified object that finally appears in the vector.
Lastindexof (object, INT)
Find the specified object from the specified subscript and return its subscript.
Removeallelements ()
Delete all elements of a vector and set its size to zero.
Removeelement (object)
Delete the first parameter from the vector.
Removeelementat (INT)
Deletes the element at the specified subscript.
Setelementat (object, INT)
Set indexThe element is the specified object.
Setsize (INT)
Set the vector size.
Size ()
Returns the number of elements of the vector.
Tostring ()
Returns the string representation of the vector.
Trimtosize ()
Adjust the capacity of the vector to the current size of the vector.


Variable

Elementdata

 protected Object elementData[]
Stores the Array Buffer of elements in a vector. The vector capacity is the length of the array buffer.

Elementcount

 protected int elementCount
Number of valid elements in the vector.

Capacityincrement

 protected int capacityIncrement
When the vector size exceeds its capacity, the vector capacity is automatically increased. If the capacity increment is 0The vector capacity is doubled every time it needs to be increased.


Constructor

Vector

 public Vector(int initialCapacity,               int capacityIncrement)
Construct an empty vector with the specified initial capacity and capacity increment.

Parameters:
Initialcapacity-initial capacity of the vector.
Capacityincrement-the capacity growth when the vector overflows.

Vector

 public Vector(int initialCapacity)
Construct an empty vector with the specified initial capacity.

Parameters:
Initialcapacity-initial capacity of the vector.

Vector

 public Vector()
Construct an empty vector.


Method

Copyinto

 public final synchronized void copyInto(Object anArray[])
Copy the element of the vector to the specified array. The array must be large enough to accommodate all objects in the vector.

Parameters:
Anarray-an array of the copied elements.

Trimtosize

 public final synchronized void trimToSize()
Adjust the capacity of the vector to the current size of the vector. The application can use this operation to minimize vector storage space.

Ensurecapacity

 public final synchronized void ensureCapacity(int minCapacity)
Increase vector capacity. If necessary, make sure that it can accommodate at least the number of components specified by the minimum capacity parameter.

Parameters:
Mincapacity-minimum capacity required.

Setsize

 public final synchronized void setSize(int newSize)
Set the vector size. If the new size is greater than the current size, the new nullItem to the end of the vector. If the new size is smaller than the current size, all subscript is greater than or equal newSizeWill be discarded.

Parameters:
Newsize-the new size of the vector.

Capacity

 public final int capacity()
Returns the current capacity of the vector.

Return Value:
The current capacity of the vector.

Size

 public final int size()
Returns the number of elements of the vector.

Return Value:
Number of elements in the vector.

Isempty

 public final boolean isEmpty()
Test whether the vector has no element.

Return Value:
If the vector has no element, return trueOtherwise false.

Elements

 public final synchronized Enumeration elements()
Returns an enumeration of the element of the vector.

Return Value:
An enumeration of the element of the vector.
See:
Enumeration

Contains

 public final boolean contains(Object elem)
The specified object in the test is an element of the vector.

Parameters:
ELEM-an object.
Return Value:
If the specified object is an element in the vector trueOtherwise false.

Indexof

 public final int indexOf(Object elem)
Find the location where a given parameter appears for the first time in the vector, and use equalsMethod to test whether they are equal.

Parameters:
ELEM-an object.
Return Value:
Subscript at the first occurrence of a parameter in the vector; if the object is not found, the return value is -1.
See:
Equals

Indexof

 public final synchronized int indexOf(Object elem,                                      int index)
Slave indexIn the left-side navigation pane. equalsMethod to test whether they are equal.

Parameters:
ELEM-an object.
Index-subscript of the start position.
Return Value:
Parameter in the vector indexThe subscript of the place where the object appears for the first time after the object is located. If the object is not found -1.
See:
Equals

Lastindexof

 public final int lastIndexOf(Object elem)
Returns the subscript of the specified object that finally appears in the vector.

Parameters:
ELEM-specified element.
Return Value:
Specifies the subscript at the last occurrence of an object in the vector. if the object is not found -1.

Lastindexof

 public final synchronized int lastIndexOf(Object elem,                                      int index)
Find the specified object from the specified subscript and return its subscript.

Parameters:
ELEM-specified element.
Index-subscript of the start position.
Return Value:
Specify the object in the vector indexThe subscript of the place where the object appears for the first time after the object is located. If the object is not found -1.

Elementat

 public final synchronized Object elementAt(int index)
Returns the component at the specified subscript.

Parameters:
Index-a subscript in the vector.
Return Value:
Specifies the element at the subscript.
Throw:Arrayindexoutofboundsexception
If the subscript is invalid.

Firstelement

 public final synchronized Object firstElement()
Returns the first element of the vector.

Return Value:
The first element of the vector.
Throw:Nosuchelementexception
If this vector has no element.

Lastelement

 public final synchronized Object lastElement()
Returns the last element of the vector.

Return Value:
The last element of the vector, that is, the subscript size() - 1.
Throw:Nosuchelementexception
If this vector is empty.

Setelementat

 public final synchronized void setElementAt(Object obj,                                      int index)
Set indexThe element is the specified object. The previous element at this location is discarded.

The value must be greater than or equal0And smaller than the current vector size.

Parameters:
OBJ-the object to which the element is set.
Index-specify the subscript.
Throw:Arrayindexoutofboundsexception
If the subscript is invalid.
See:
Size

Removeelementat

 public final synchronized void removeElementAt(int index)
Deletes the element at the specified subscript. In this vector, the subscript is greater than or equal indexThe value of the element is reduced by one.

The value must be greater than or equal0And smaller than the current vector size.

Parameters:
Index-subscript of the object to be deleted.
Throw:Arrayindexoutofboundsexception
If the subscript is invalid.
See:
Size

Insertelementat

 public final synchronized void insertElementAt(Object obj,                                      int index)
In indexInsert a specified object as the vector element. In this vector, the subscript is greater than or equal indexThe value of the element plus one.

The value must be greater than or equal0And is smaller than or equal to the current vector size.

Parameters:
OBJ-elements to be inserted
Index-insert the position of the new element.
Throw:Arrayindexoutofboundsexception
If the subscript is invalid.
See:
Size

Addelement

 public final synchronized void addElement(Object obj)
Add a specified component at the end of the vector and add the length to the length. When the vector size exceeds its capacity, the vector capacity automatically increases.

Parameters:
OBJ-the element to be added.

Removeelement

 public final synchronized boolean removeElement(Object obj)
Delete the first parameter from the vector. If this object is found in the vector, the subscript in the vector is greater than or equal indexThe value of each element is reduced by one.

Parameters:
OBJ-the element to be deleted.
Return Value:
If the parameter is an element of a vector, return trueOtherwise false.

Removeallelements

 public final synchronized void removeAllElements()
Delete all elements of a vector and set its size to zero.

Clone

 public synchronized Object clone()
Returns a copy of the vector.

Return Value:
A copy of the vector.
Overwrite:
Clone in Class Object

Tostring

 public final synchronized String toString()
Returns the string representation of the vector.

Return Value:
String Representation of the vector.
Overwrite:
Tostring in Class Object

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.