Detailed explanation of Vector in Collection

1. Introduction to Vector

  1. java.util.vector provides a vector class (Vector) to achieve a function similar to a dynamic array; after creating a vector class object, you can insert objects of different classes at will, regardless of the type or pre-selected capacity, and can be implemented conveniently Find.
  2. Adapt to the scene: If you don't know or don't want to define the size of the array in advance, and you need to find, insert, and delete frequently, you can consider using vector classes.
  3. The vector class provides three construction methods:
public vector() 
public vector(int initialcapacity,int capacityIncrement) 
public vector(int initialcapacity)

Using the first construction method, the system will automatically manage the vector, using the latter two construction methods, the system will set the capacity according to the given parameters.

2. Insert function

  1. public final synchronized void adddElement(Object obj)
    Insert the obj object into the end of the vector. Obj can be any type of object or other types of objects. When you insert a value, you need to convert it to an object.
Vector v1 = new Vector(); 
Integer integer1 = new Integer(1); 
v1.addElement(integer1); 
  1. public final synchronized void setElementAt(Object obj,int index)
    Set the object at the index position to obj, overwriting the original object.
  2. public final synchronized void insertElementAt(Object obj,int index)
    Insert obj at the position specified by index, and the original object and subsequent objects will be postponed sequentially.

Three, delete function

  1. public final synchronized void removeElement(Object obj)
    Delete obj from the vector. If there are more than one, start from the beginning of the vector and delete the first vector member that is the same as obj.
  2. public final synchronized void removeAllElement();
    Delete all objects in the vector
  3. public fianl synchronized void removeElementAt(int index)
    Delete the object at the place pointed by index

Four, query search function

indexOf(obj)
indexOf(obj,index)
lastindexOf(obj)
lastIndex(obj,index)
firstElement()
lastElement()

Guess you like

Origin blog.csdn.net/Cxf2018/article/details/109326078