Java.util.Vector

Source: Internet
Author: User

 Public class Vector<e>    extends abstractlist<e>    implements List<e>, Randomaccess, Cloneable, java.io.Serializable

Instance variable

// container for saving elements protected object[] Elementdata; //Number of elements protected int Elementcount; // increment of container when expanding protected int capacityincrement;

4 constructors

//constructors for initial capacity and container expansion increments PublicVector (intInitialcapacity,intcapacityincrement) {        Super(); if(Initialcapacity < 0)            Throw NewIllegalArgumentException ("Illegal capacity:" +initialcapacity);  This. Elementdata =NewObject[initialcapacity];  This. capacityincrement =capacityincrement;}//only the initial capacity of the constructor, the default use of container expansion increment of 0, indicating not specified, then the capacity will become twice times the original volume PublicVector (intinitialcapacity) {         This(initialcapacity, 0);} Without specifying the initial capacity, the default initial capacity is ten PublicVector () { This(10);}//using C to construct vectors PublicVector (collection<?extendsE>c) {Elementdata=C.toarray (); Elementcount=elementdata.length; //C.toarray might (incorrectly) not return object[] (see 6260652)        if(Elementdata.getclass ()! = object[].class) Elementdata= Arrays.copyof (Elementdata, Elementcount, object[].class);}

Expansion method

 Public synchronized voidEnsurecapacity (intmincapacity) {        if(Mincapacity > 0) {Modcount++;        Ensurecapacityhelper (mincapacity); }}Private voidEnsurecapacityhelper (intmincapacity) {        //overflow-conscious Code        if(Mincapacity-elementdata.length > 0) grow (mincapacity);}Private voidGrowintmincapacity) {        //overflow-conscious Code        intOldcapacity =elementdata.length; intNewcapacity = Oldcapacity + ((capacityincrement > 0)?capacityincrement:oldcapacity);//if Capacityincrement is 0, then newcapacity is oldcapacity * 2        if(Newcapacity-mincapacity < 0) newcapacity=mincapacity; if(Newcapacity-max_array_size > 0) newcapacity=hugecapacity (mincapacity); Elementdata=arrays.copyof (Elementdata, newcapacity);}

Set capacity

 Public synchronized voidSetSize (intnewSize) {Modcount++; if(NewSize > Elementcount) {//If the new capacity is larger than the old capacity, the expansionEnsurecapacityhelper (newSize); } Else{//Otherwise, the portion that exceeds the new capacity is set to NULL, noting that the capacity has not changed, except that the element that exceeds the newsize becomes null             for(inti = newSize; i < Elementcount; i++) {Elementdata[i]=NULL; }} Elementcount=newSize;}

2 iterators, one from the beginning to the end, and one from the end iteration to the head

Private classItrImplementsIterator<e>{//forward iterator, cursor 0intCursor//index of next element to return        intLastret =-1;//index of last element returned;-1 if no such        intExpectedmodcount =Modcount;  Public BooleanHasnext () {//Racy But within spec, since modifications is checked//within or after synchronization in Next/previous            returnCursor! =Elementcount; }         PublicE Next () {synchronized(Vector. This) {checkforcomodification (); inti =cursor; if(I >=elementcount)Throw Newnosuchelementexception (); Cursor= i + 1; returnElementdata (Lastret =i); }        }         Public voidRemove () {if(Lastret = =-1)                Throw Newillegalstateexception (); synchronized(Vector. This) {checkforcomodification (); Vector. This. Remove (Lastret); Expectedmodcount=Modcount; } cursor=Lastret; Lastret=-1; } @Override Public voidForeachremaining (consumer<?SuperE>action)            {Objects.requirenonnull (action); synchronized(Vector. This) {                Final intSize =Elementcount; inti =cursor; if(I >=size) {                    return; } @SuppressWarnings ("Unchecked")                FinalE[] Elementdata = (e[]) Vector. This. Elementdata; if(I >=elementdata.length) {Throw Newconcurrentmodificationexception (); }                 while(I! = size && Modcount = =Expectedmodcount) {action.accept (elementdata[i++]); }                //update once at end of iteration to reduce heap write trafficcursor =i; Lastret= I-1;            Checkforcomodification (); }        }        Final voidcheckforcomodification () {if(Modcount! =expectedmodcount)Throw Newconcurrentmodificationexception (); }}

Final classListitrextendsItrImplementsListiterator<e>{//From index to iteration listitr (intindex) {            Super(); Cursor=index; }         Public Booleanhasprevious () {returnCursor! = 0; }         Public intNextindex () {returncursor; }         Public intPreviousindex () {returnCursor-1; }         PublicE Previous () {synchronized(Vector. This) {checkforcomodification (); inti = Cursor-1; if(I < 0)                    Throw Newnosuchelementexception (); Cursor=i; returnElementdata (Lastret =i); }        }         Public voidset (e e) {if(Lastret = =-1)                Throw Newillegalstateexception (); synchronized(Vector. This) {checkforcomodification (); Vector. This. Set (Lastret, E); }        }         Public voidAdd (e e) {inti =cursor; synchronized(Vector. This) {checkforcomodification (); Vector. This. Add (i, E); Expectedmodcount=Modcount; } cursor= i + 1; Lastret=-1; }} @Override Public synchronized voidForEach (consumer<?SuperE>action)        {Objects.requirenonnull (action); Final intExpectedmodcount =Modcount; @SuppressWarnings ("Unchecked")        FinalE[] Elementdata = (e[]) This. Elementdata; Final intElementcount = This. Elementcount;  for(inti=0; Modcount = = Expectedmodcount && i < elementcount; i++) {action.accept (elementdata[i]); }        if(Modcount! =Expectedmodcount) {            Throw Newconcurrentmodificationexception (); }}

// index Specifies the position from which to start the forward iteration  Public synchronized Listiterator<e> listiterator (int  index) {        if (Index < 0 | | | index >  Elementcount)            thrownew indexoutofboundsexception ("index:" +index);         return New Listitr (index);}

Vector is thread-safe, its methods are synchronized decorated, and if thread-safe is not required, ArrayList is recommended instead.

Java.util.Vector

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.