Home  >  Article  >  Java  >  Study notes on vector class usage in Java programming

Study notes on vector class usage in Java programming

黄舟
黄舟Original
2017-01-20 11:05:191659browse

Vector is usually used to implement dynamic arrays, that is, to implement automatically growing object arrays. Like C++, the vector class is also built into Java. Let’s take a look at the basic usage of the vector class.

java.util.vector provides a vector class (vector) to implement functions similar to dynamic arrays. There is no concept of pointers in the Java language, but if pointers are used correctly and flexibly, the quality of the program can indeed be greatly improved. For example, so-called "dynamic arrays" in C and C++ are generally implemented by pointers. In order to make up for this shortcoming, Java provides a rich class library to facilitate programmers to use, and the vector class is one of them. In fact, flexible use of arrays can also complete the functions of the vector class, but the vector class provides a large number of methods, which greatly facilitates user use.
After creating a vector class object, you can insert objects of different classes into it at will, without taking into account the type or pre-selecting the capacity of the vector, and you can easily search. For situations where the array size is unknown or unwilling to be defined in advance, and frequent searches, insertions, and deletions are required. Consider using vector classes.
The Vector class implements a dynamic array. Similar to ArrayList, but different:

Vector is accessed synchronously.

Vector contains many traditional methods that are not part of the collection framework.

Vector is mainly used when the size of the array is not known in advance, or when an array that can be changed is needed.

The Vector class supports 4 construction methods.

1. The first construction method creates a default vector with a default size of 10:

Vector()
2. The second construction method creates a vector of the specified size.

Vector(int size)
3. The third construction method creates a vector of the specified size, and the increment is specified by incr. The increment represents the number of elements added to the vector each time.

Vector(int size,int incr)
4. The fourth construction method creates a vector containing elements of collection c:

Vector(Collection c)

If you use the first method, the system will automatically manage the vector. If you use the latter two methods. Then the system will set the capacity of the vector object (that is, the size of the data that the vector object can store) based on the parameter initialcapacity. When the actual number of stored data exceeds the capacity. The system will expand the vector object storage capacity.

The parameter capacityincrement gives the expansion value of each expansion. When capacityincrement is 0, it will be doubled every time. This function can be used to optimize storage. Various methods are provided in the Vector class to facilitate user use:

Insertion function:
(1) public final synchronized void adddElement(Object obj)
Insert obj into the tail of the vector. obj can be any type of object. For the same vector object, objects of different types can also be inserted into it. However, objects should be inserted instead of values, so when inserting values, be careful to convert the array into the corresponding object.
For example: when you want to insert the integer 1, do not call v1.addElement(1) directly. The correct method is:

Vector v1 = new Vector(); 
Integer integer1 = new Integer(1); 
v1.addElement(integer1);

(2)public final synchronized void setElementAt(Object obj,int index)
Set the object at index to obj, and the original object will be overwritten.
(3)public final synchronized void insertElement(Object obj,int index)
Insert obj at the position specified by index, and the original object and subsequent objects are postponed in sequence.

Delete function:
(1)public final synchronized void removeElement(Object obj)
Delete obj from the vector. If there are multiple ones, try starting from the vector head and delete the found first one. A vector member identical to obj.
(2)public final synchronized void removeAllElement();
Delete all objects in the vector
(3)public fianl synchronized void removeElementAt(int index)
Delete the object pointed to by index

Query search function:
(1)public final int indexOf(Object obj)
Start searching for obj from the vector head and return the subscript corresponding to the first obj encountered. If this does not exist obj, returns -1.
(2)public final synchronized int indexOf(Object obj,int index)
Start searching for obj from the subscript represented by index.
(3)public final int lastindexOf( Object obj)
Search obj in reverse from the end of the vector.
(4)public final synchornized int lastIndex(Object obj,int index)
Search obj in reverse from the end to the beginning at the subscript represented by index .
(5)public final synchornized firstElement()
Get the first obj in the vector object
(6)public final synchornized Object lastElement()
Get the last obj of the vector object

Example
The following program illustrates several methods supported by this collection:

import java.util.*;
 
public class VectorDemo {
 
  public static void main(String args[]) {
   // initial size is 3, increment is 2
   Vector v = new Vector(3, 2);
   System.out.println("Initial size: " + v.size());
   System.out.println("Initial capacity: " +
   v.capacity());
   v.addElement(new Integer(1));
   v.addElement(new Integer(2));
   v.addElement(new Integer(3));
   v.addElement(new Integer(4));
   System.out.println("Capacity after four additions: " +
     v.capacity());
 
   v.addElement(new Double(5.45));
   System.out.println("Current capacity: " +
   v.capacity());
   v.addElement(new Double(6.08));
   v.addElement(new Integer(7));
   System.out.println("Current capacity: " +
   v.capacity());
   v.addElement(new Float(9.4));
   v.addElement(new Integer(10));
   System.out.println("Current capacity: " +
   v.capacity());
   v.addElement(new Integer(11));
   v.addElement(new Integer(12));
   System.out.println("First element: " +
     (Integer)v.firstElement());
   System.out.println("Last element: " +
     (Integer)v.lastElement());
   if(v.contains(new Integer(3)))
     System.out.println("Vector contains 3.");
   // enumerate the elements in the vector.
   Enumeration vEnum = v.elements();
   System.out.println("\nElements in vector:");
   while(vEnum.hasMoreElements())
     System.out.print(vEnum.nextElement() + " ");
   System.out.println();
  }
}

The compilation and running results of the above example are as follows:

Initial size: 0
Initial capacity: 3
Capacity after four additions: 5
Current capacity: 5
Current capacity: 7
Current capacity: 9
First element: 1
Last element: 12
Vector contains 3.
 
Elements in vector:
1 2 3 4 5.45 6.08 7 9.4 10 11 12

The above is the usage of vector class in Java programming For the content of study notes, please pay attention to the PHP Chinese website (www.php.cn) for more related content!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn