(1) is used without declaring the upper limit. With the increase of elements, the length of the vector will automatically increase;
② Vector class provides additional methods to add and delete elements, which is more efficient than array operation. ( 1)public final synchronized void addElement(Object obj)
Insert obj at the end of the vector. An obj can be an object of any class. For the same vector object, you can insert objects of different classes. But what you want to insert is an object, not a numerical value, so when inserting a numerical value, you should pay attention to converting the numerical value into the corresponding object.
For example, when you want to insert an integer of 1, don't call v 1.addElement( 1) directly. The correct method is: vectorv1= newvector (); integer integer 1 = new integer( 1); v 1 . add element(integer 1); (2)public final synchronization void setElementAt(object obj, int index)
Set the object at index to obj, and the original object will be overwritten.
(3)public final synchronization void insertElementAt(Object obj, int index)
Insert obj at the position specified by index, and the original object and subsequent objects are delayed in turn. (1)public final synchronization void removeElement(Object obj)
Delete obj from the vector. If there are more than one, try from the vector header to delete the first vector member found to be the same as obj.
(2)public final synchronized void remove allement()
Delete all objects in the vector.
(3)public final synchronization void removeElementlAt(int index)
Deletes the object at the location indicated by the index. ( 1)public final int index of(Object obj)
Search for obj from the vector header and return the subscript corresponding to the first obj encountered. If there is no obj, return-1.
(2)public final synchronized int index of(Object obj,int index)
Search obj from the subscript indicated by index.
(3)public final int lastIndexOf(Object obj)
Search obj backwards from the tail of the vector.
(4) public final synchronized int lastindexof (object obj, int index)
Starting from the subscript indicated by the index, the object is searched backwards from the tail to the head.
(5)public final synchronization object firstElement ()
Gets the first obj in the vector object.
(6) public final synchronization object lastelement ()
Gets the last obj in the vector object. After understanding the most basic method of vectors, let's take a look at VectorApp.java's example.
Example VectorApp.java importjava.util.vector; import Java . lang . *; //This sentence should not be used, but the original text is so importjava.util.enumeration; Public? Class? VectorApp{public? Static electricity Invalid? main(String/* * * themethoinsertelementat(Integer,int)in thetypevector & lt; Integer & gtis not applicable for * arguments (string, int) * Insertelement () means: insertspecific object component Thisvectorat * specific index. *//Convert v 1 into a string and print V 1. Insertelement (three,2); v 1 . insertelementat(new float(3.9),3); System. out.println (vectorv1(usedmethodinsertelementat ()) is: \ n \ t+v1); //above, please change it to vector.
e:\ Java 0 1 & gt; java VectorApp
The vector v 1 is:
[1, 1, 1, 2, 2, 1, 1]
The vector v 1 (using the method insertElementAt ()) is:
[1, 1, 3.9, 1, 2, 2, 1, 1]
The vector v 1 (using the method setElementAt ()) is:
[1, 1, 4, 3.9, 1, 2, 2, 1, 1]
The vector v 1 (using the method removeElement ()) is:
One 4 3.9 1 two 2 1 1
Location of object 1 (from top to bottom): 3
Location of object 1 (from top to bottom): 7
The new vector (resized) is:
[One, four, 3.9, 1]
e:\ Java 0 1 & gt;
From the running results of the example 1, we can clearly understand the functions of the above methods, and there are still some points to be explained.
The (1) class vector defines this method.
public final int size()
This method is used to get the number of vector elements. Its return value is the number of elements actually existing in the vector, not the vector capacity. You can call the capacity () method to get the capacity value.
Method:
public final synchronized void setsize(int newsize)
This method is used to define the vector size. If the number of existing members of the vector object exceeds the value of newsize, the excess elements will be lost.
(2) An object of enumeration class is defined in the program.
Enumeration is an interface class in java.util, which encapsulates the method of enumerating data sets.
The enumeration provides the hawMoreElement () method to determine whether there are other elements in the collection, and the nextElement () method to get the next element. These two methods can be used to get the elements in the collection in turn.
Methods provided in Vector:
Common Final Synchronization Enumeration Element ()
This method maps a vector object to an enumeration type. Most other classes in the java.util package also have this method, which is convenient for users to obtain the corresponding enumeration types.