Java Learning Note 27

Source: Internet
Author: User

Vector is the implementation class of the list interface, supports all functions of the list interface, the vector class is an array-based implementation of the list class, in the internal encapsulation of a dynamic,


Allow redistribution of the object[] array, vector is thread-safe, without the need for a program to ensure the synchronization of the collection.


The following is a description of the methods used by the vector class:


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 Volume:" +vector.capacity ());/* * Increase the capacity of this vector (if necessary) to ensure that it can hold at least the number of components specified by the minimum capacity parameter. * If the current capacity of this vector is less than mincapacity, increase its capacity by replacing its internal data array (saved in field Elementdata) with a larger array. The size of the new data array will be the original size plus capacityincrement, * unless the capacityincrement value is less than or equal to zero, in the latter case the new capacity will be twice times the original capacity, * However, if this size is still less than mincapacity,  The new capacity will be mincapacity. */vector.ensurecapacity (21);//output: Vector Volume: 21system.out.println ("Vector Volume:" +vector.capacity ()); Vector vector1=new vector (), Vector1.add (list),/* * If the specified Object is equal to this vector, returns True */system.out.println (Vector.equals (  Vector1));//output: true/* * Returns the item at the first component of this vector (at 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 range (Index < 0 | | index >= size ()), throw arrayindexoutofboundsexception exception */system.out.println (vector.get (0)) ;//output: [bill]/* * Returns the hash code for this vectorValue. */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 first appears in this vector, or 1 if the vector does not contain the element.  To be more exact, * Returns the lowest index of the satisfied (o==null. Get (i) ==null:o.equals (get (i))) and returns-1 if there is no such index. */system.out.println (Vector.indexof ("JACK"));//output: 1/* * Returns the index of the specified element that first appears in this vector, and a forward search from index, * returns 1 if the element is not found. * If the specified index is negative, throw indexoutofboundsexception exception */system.out.println (Vector.indexof ("JACK", 2));//output: 3/* * Inserts the specified object as a component in this vector at the specified index. * Each index in this vector that is greater than or equal to the specified index will be shifted upward, making its index value 1 larger than the previous value.  The index must be a value greater than or equal to 0 and * less than or equal to the current size of the vector (if the index equals the current size of the vector, * adds the new element to the vector). * If the index is out of range (Index < 0 | | index > Size ()), throw arrayindexoutofboundsexception exception */vector.insertelementat ("WORD", 1);// Output: [[BILL], WORD, JACK, Marray, jack]system.out.println (vector),/* * Returns TRUE if and only if this vector has no component (that is, its size is 0); */system.out.println (Vector.isempty ());//output: false/* * Returns the index of the last occurrence of the specified element in this vector, or 1 if the vector does not contain the element. To be more exact, * Return to meet (o==null ?  The highest index i * for Get (i) ==null:o.equals (get (i)), or 1 if there is no such index. */system.out.println (Vector.lastindexof ("JACK"));//output: 4/* * Removes the element at the specified position in this vector. Shifts all subsequent elements to the left (index minus 1).  Returns the element that is removed from this vector. * If the index is out of range (Index < 0 | | index >= size ()), throw arrayindexoutofboundsexception exception */vector.remove (0);//output: [WORD, JACK, M ARRAY, jack]system.out.println (vector);/* Removes the first occurrence of the specified element in this vector, and if the vector does not contain the element, the element remains unchanged.  * To be more exact, remove the element whose index I satisfies (o==null. Get (i) ==null:o.equals (get (i))) * (if such an element exists). */system.out.println (Vector.remove ("WORD")); System.out.println (vector);//output: [Jack, Marray, Jack]}}

For more information on vectors, see the following articles




Reprint Please specify source:http://blog.csdn.net/hai_qing_xu_kong/article/details/44106381 Emotional Control _  

Java Learning Note 27

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.