Java Study Notes 27, java Study Notes
Vector is the implementation class of the List interface and supports all functions of the List interface. The Vector class is a List class implemented based on arrays and encapsulates a dynamic,
The Object [] array that allows redistribution. The Vector is thread-safe and requires no program to ensure the synchronization of the set.
Some methods of the Vector class are described as follows:
Public class Main {public static void main (String [] args) {Vector vector = new Vector (); ArrayList list = new ArrayList (); list. add ("BILL"); vector. add (list); // output: [[BILL] System. out. println (vector); // output: vector capacity: 10System. out. println ("vector capacity:" + vector. capacity ();/** increase the capacity of this vector (if necessary) to ensure that it can save at least the number of components specified by the minimum capacity parameter. * If the current capacity of this vector is smaller than minCapacity, replace its internal data array (saved in the field elementData) * with a large array to increase its capacity. The size of the new data array will add capacityIncrement to the original size. * unless the value of capacityIncrement is smaller than or equal to zero, in the latter case, the new capacity will be twice the original capacity, * However, if the size is still smaller than minCapacity, The new capacity will be minCapacity. */Vector. ensureCapacity (21); // output: vector capacity: 21System. out. println ("vector capacity:" + vector. capacity (); Vector vector1 = new Vector (); vector1.add (list);/** returns true */System if the specified Object is equal to this Vector. out. println (vector. equals (vector1); // output: true/** returns the item at the first component of the vector (index 0 ). */System. out. println (vector. firstElement (); // output: [BILL]/** returns the element at the specified position in the vector. * If the index is out of the range (index <0 | index> = size (), an ArrayIndexOutOfBoundsException exception is thrown */System. out. println (vector. get (0); // output: [BILL]/** returns the hash value of this vector. */System. out. println (vector. hashCode (); vector. add ("JACK"); vector. add ("MARRAY"); vector. add ("JACK"); // output: [[BILL], JACK, MARRAY, JACK] System. out. println (vector);/** returns the index of the specified element that appears for the first time in this vector. If this vector does not contain this element,-1 is returned. More specifically, * returns true (o = null? Get (I) = null: the lowest index I of o. equals (get (I); * if no such index exists,-1 is returned. */System. out. println (vector. indexOf ("JACK"); // output: 1/** returns the index of the specified element that appears for the first time in this vector. The index is positively searched, * If this element is not found,-1 is returned. * If the specified index is negative, an IndexOutOfBoundsException exception is thrown */System. out. println (vector. indexOf ("JACK", 2); // output: 3/** Insert the specified object as a component in this vector to the specified index. * Each component in this vector whose index is greater than or equal to the specified index will shift up. * change the index value to a value greater than 1 in the past. The index must be a value greater than or equal to 0 and * less than or equal to the current vector size (if the index is equal to the current vector size, * the new element is added to the vector ). * If the index is out of the range (index <0 | index> size (), an ArrayIndexOutOfBoundsException exception is thrown */vector. insertElementAt ("WORD", 1); // output: [[BILL], WORD, JACK, MARRAY, JACK] System. out. println (vector);/** returns true if and only if this vector has no components (that is, its size is zero); otherwise, false. */System. out. println (vector. isEmpty (); // output: false/** returns the index of the last occurrence of the specified element in this vector. If this vector does not contain this element,-1 is returned. More specifically, * returns true (o = null? Get (I) = null: the highest index I * of o. equals (get (I); if no such index exists,-1 is returned. */System. out. println (vector. lastIndexOf ("JACK"); // output: 4/** remove the element at the specified position in the vector. Remove all subsequent elements to the left (delete the index by 1 ). Returns the removed element from the vector. * If the index is out of the range (index <0 | index> = size (), an ArrayIndexOutOfBoundsException exception is thrown */vector. remove (0); // output: [WORD, JACK, MARRAY, JACK] System. out. println (vector);/** removes the first matching item of the specified element in this vector. If the vector does not contain this element, the element remains unchanged. * To be more precise, the removal of its index I satisfies (o = null? Get (I) = null: o. equals (get (I) * element (if such an element exists ). */System. out. println (vector. remove ("WORD"); System. out. println (vector); // output: [JACK, MARRAY, JACK]}
For more information about the Vector method, see subsequent articles.
Reprinted please indicate the source: http://blog.csdn.net/hai_qing_xu_kong/article/details/44106381 sentiment control _