The Vector class, called a Vector class, implements dynamic arrays for objects with varying numbers of elements. Like arrays, vectors use subscripts starting at 0 to indicate the positions of elements; Unlike arrays, however, when a vector is created, the number of elements in the array automatically changes as the number of elements in the vector increases or decreases.

Vector has a data structure similar to ArrayList. It contains three member variables: elementData, elementCount, and capacityIncrement.

(01) elementData is an “array of type Object[]” that holds elements added to a Vector. ElementData is a dynamic array. If the > size of the dynamic array is not specified when initializing the Vector, the default size 10 is used. As the elements in the Vector increase, the capacity of the Vector also grows dynamically. CapacityIncrement is a growth coefficient related to capacity growth. For specific growth methods, please refer to the ensureCapacity() function in source code analysis.

ElementCount is the actual size of the dynamic array.

CapacityIncrement is the growth coefficient of dynamic array. If capacityIncrement is specified when creating the Vector; Then, each time the dynamic array capacity in the Vector increases >, the increment is capacityIncrement.

However, because its underlying data structure is array, query fast, add and delete full, thread is not safe, so it is not often used, and ArrayList is usually used instead of it.

The Stack class,

Stack inheritance parent class, interface and overview

                             

Stack class, as the Implementation of Java Stack structure, has the characteristics of advanced last out, last in first out. It is based on the Vector class. The Stack class itself has five common methods for the Stack (pushing the Stack, removing the Stack, looking at the top of the Stack, determining whether the Stack is empty, and looking for specified elements in the Stack). In addition to these methods, it can also use the public methods of its parent Vector class. But for a stack, basic pushing out of the stack to find elements is enough.

Second, method explanation

1. Push (E item)

Public E push(E item) {addElement(item); public E push(E item) {addElement(item); return item; }Copy the code

2. The stack pop ()

Public synchronized E pop() {synchronized E pop(); synchronized E pop(); int len = size(); obj = peek(); removeElementAt(len - 1); return obj; }Copy the code

3. Check the top stack element peek()

    public synchronized E peek() {
        int     len = size();

        if (len == 0)
            throw new EmptyStackException();
        return elementAt(len - 1);
    }
Copy the code

4. Check whether the stack is isEmpty()

    public boolean empty() {
        return size() == 0;
    }
Copy the code

5. Search (Object obj)

    public synchronized int search(Object o) {
        int i = lastIndexOf(o);

        if (i >= 0) {
            return size() - i;
        }
        return -1;
    }
Copy the code

Stack has only five methods, and all of its implementations are based on Vector methods.

Third, basic use Demo

    @Test
    public void test1() {
        Stack<String> stack = new Stack<>();
        stack.push("hello");
        stack.push("world");
        stack.push("java");
        
        System.out.println("empty:" + stack.empty());
        System.out.println("peek:" + stack.peek());
        System.out.println("peek:" + stack.search("world"));

        while (!stack.isEmpty()) {
            System.out.println(stack.pop());
        }
    }
Copy the code

\