Java Basics - Collections Framework --list the Vector class

I. Introduction
Before JDK2 (prior to Java frame set), a plurality of data to be stored, then there is a class called Vector.
Vector class is actually a bottom array of Object, Vector class supports synchronous method (method using synchronized decorative.

The design principle two .vector (view source):
Here Insert Picture Description
three .Vector storage principle categories:
through source code analysis, found to have a type Object [] array Vector class.

protected Object[] elementData;

1): Data is stored on the surface of the object to the Vector, in fact, the underlying data is still stored in the Object array.
2): We have found the elements of the array type is Object type, meaning that the collection can store any type of objects .
collection can store objects, can not store the value of the basic data types.
before Java5, must manually packing for basic data types.
as: v.addElement (Integer.valueOf (123)) ;
from the beginning Java5 support autoloader box operation, code
such as:. v.addElement (123); the bottom is actually still manually packing
. Note: compiler level above item to modify or Java5 Java5 (right)
3): class objects stored in the collection are stored is a reference to the object, not the object itself.

public class MyVector {
    public static void main(String[] args) {
        Vector v =  new Vector(5);
        StringBuilder sb = new StringBuilder("ABC");
        v.add(sb);
        System.out.println(v);//[ABC]
        sb.append("DEF");
        System.out.println(v);//[ABCDEF]
    }
}

Four common method for
increasing:
Boolean the Add (Object E) to the end of the specified element vector is equivalent to the addElement.
void add (int index, Object element ) at a predetermined position of this vector is inserted into the specified element.
boolean addAll (Collection c): c add collection element to the current collection object.

Delete:
Object Remove (int index): Delete the specified index of the element, and then return the element deletion.
Boolean Remove (Object O):. Remove a specified element
boolean removeAll (Collection c): removing from the collection of a specified the set of all the elements in c.
boolean retainAll (Collection c): In this collection retain only the elements contained in the specified set of c, the intersection of two sets.

Review:
Object SET (int index, Object Element): Modify index location specified in the element of the current set.
Returns replaced the old elements.

Query:
int size (): returns the number of elements stored in the current collection.
Boolean isEmpty (): determining whether the current number of elements in the set is of 0. The
Object GET (int index):. A query element specified index
Object [] toArray (): Object to convert a collection of object arrays.

Published 99 original articles · won praise 2 · Views 2610

Guess you like

Origin blog.csdn.net/weixin_41588751/article/details/105252884