Arraylist vs vector (the difference between arraylist and vector) _ often appears during interviews

Source: Internet
Author: User
Data Growth:When you need to increase, VectorThe default growth is the original one training, andArraylistHalf of the original
Synchronization: the vector is thread-safe, that is, synchronous, And the arraylist is a line.ProgramNot secure, not synchronous

Here we can look at their source code to know:

The size () method of arraylist:

1 /**2 * Returns the number of elements in this list.3 *4 *@ ReturnThe number of elements in this list5*/6Public IntSize (){7ReturnSize;8}

The size () method of vector:

  1  /**   2   * returns the number of components in this vector.   3   *   4   *   @ return   the number of components in this vector   5   */  6   Public   synchronized   int   size () {  7   return   elementcount;   8 }

In fact, many other methods of vector are synchronized:

 1   /**  2   * Sets the size of this vector. If the new size is greater than  3   * Current size, new {  @ Code  Null} items are added to the end  4   * The vector. If the new size is less than the current size, all  5   * Components at index {  @ Code Newsize} and greater are discarded.  6   *  7   *  @ Param  Newsize the new size of this vector  8   *  @ Throws  Arrayindexoutofboundsexception if the new size is negative  9        */  10       Public  Synchronized   Void Setsize ( Int  Newsize ){  11 Modcount ++ ;  12           If (Newsize> Elementcount ){  13   Ensurecapacityhelper (newsize );  14 } Else  {  15              For ( Int I = newsize; I <elementcount; I ++ ){  16 Elementdata [I] = Null  ;  17   }  18   }  19 Elementcount = Newsize;  20   }  21  22       /**  23   * Returns the current capacity of this vector.  24   *  25   *  @ Return  The current capacity (the length of its internal  26   * Data array, kept in the field {  @ Code  Elementdata}  27  * Of this vector)  28        */  29       Public   Synchronized   Int  Capacity (){  30           Return  Elementdata. length;  31   }  32   33       /**  34  * Returns the number of components in this vector.  35   *  36   *  @ Return  The number of components in this vector  37        */  38       Public   Synchronized   Int  Size (){  39           Return Elementcount;  40   }  41   42       /**  43   * Tests if this vector has no components.  44   *  45   *  @ Return  {  @ Code  True} if and only if this vector has 46   * No components, that is, its size is zero;  47   *{  @ Code  False} otherwise.  48        */  49       Public   Synchronized   Boolean  Isempty (){  50           Return Elementcount = 0;  51 }

Now we should design a scheme to study the correctness of our conclusions !!

The following shows the arraylist thread unsafe Proof Scheme:

 1   /**  2   *  3    */  4   Package  Com. b510;  5   6   Import  Java. util. arraylist; 7   Import  Java. util. collections;  8   Import  Java. util. List;  9   10   /**  11   * @ Date 2013-5-2  12   *  @ Author  Xhw  13   * 14    */  15   Public   Class Hongtenarraylist Implements  Runnable {  16       //  Thread Security  17 List <string> List = New Arraylist <string> (1 );  18       //  Thread Security 19       //  List <string> List = collections. synchronizedlist (New arraylist <string> ());  20       Public   Void  Run (){  21           Try  {  22 Thread. Sleep (( Int ) (Math. Random () * 2 ));  23 } Catch (Interruptedexception e ){  24   E. printstacktrace ();  25   }  26   System. Out. println (thread. currentthread (). getname ());  27   List. Add (thread. currentthread (). getname ());  28   }  29   30       Public   Static  Void Main (string [] ARGs) Throws  Interruptedexception {  31 Threadgroup group = New Threadgroup ("hongtengroup" );  32 Hongtenarraylist T = New  Hongtenarraylist ();  33           For ( Int I = 0; I <10000; I ++ ){ 34 Thread th = New  Thread (group, T, String. valueof (I ));  35   Th. Start ();  36   }  37           While (Group. activecount ()> 0 ){  38 Thread. Sleep (10 );  39   }  40 System. Out. println ("result =" );  41           //  If the thread is secure, the result is 10000. If the thread is not secure, the result is unpredictable.  42   System. Out. println (T. List. Size ());  43   }  44   45 }

Some running results are shown below:

Thread security:

Of course, thread security will appear below: (the running results are all 10000)

 
998299889990997999819977996599719848984699899993999196789995999299989994999699979999Result=============== 10000

For growth, we can look at the source code of arraylist:

Arraylist: Grow ()

 1   /**  2   * Increases the capacity to ensure that it can hold at least  3   * Number of elements specified by the minimum capacity argument.  4   *  5   *  @ Param  Mincapacity the desired minimum capacity  6       */  7       Private   Void Grow ( Int  Mincapacity ){  8           //  Overflow-conscious code  9           Int Oldcapacity = Elementdata. length;  10           Int Newcapacity = oldcapacity + (oldcapacity> 1 ); 11           If (Newcapacity-mincapacity <0 )  12 Newcapacity = Mincapacity;  13           If (Newcapacity-max_array_size> 0 )  14 Newcapacity = Hugecapacity (mincapacity );  15           //  Mincapacity is usually close to size, so this is a win: 16 Elementdata = Arrays. copyof (elementdata, newcapacity );  17 }

Vector: Grow ()

 1   Private   Void Grow ( Int  Mincapacity ){  2           //  Overflow-conscious code  3           Int Oldcapacity = Elementdata. length; 4           Int Newcapacity = oldcapacity + (capacityincrement> 0 )? 5   Capacityincrement: oldcapacity );  6           If (Newcapacity-mincapacity <0 )  7 Newcapacity = Mincapacity;  8           If (Newcapacity-max_array_size> 0 )  9 Newcapacity = Hugecapacity (mincapacity );  10 Elementdata = Arrays. copyof (elementdata, newcapacity );  11 }

 

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.