Comparison between Java arraylist and. Net list

Source: Internet
Author: User

Today, I saw a garden friend write a very detailed article.ArticleArraylist source code analysis, I admire writing so carefully and in detail.

While reading this article, I also and. net list <t> is compared, because I am very familiar with the implementation of list <t>, I wrote this article to illustrate the differences in their respective implementations.

    • Storage
 
//JavaPrivate TransientObject [] elementdata;

Although this type of Java is generic, it does not use Generic Array Storage internally and is not as good as. net;

 
//. NetPrivateT [] _ items;
    • Default Construction
 
//JavaPublicArraylist (){This(10 );

Java builds an array of 10 by default. In fact, in many cases, we do not store data at all. net is 0 by default, and a static 0 array instance is used ,. net is much smarter.

 
//. NetPublicList (){This. _ Items = List <t>. _ Emptyarray ;}
    • Add data
//JavaPublic BooleanAdd (e) {ensurecapacity (size+ 1 );//Increments modcount !!Elementdata [size ++] =E;Return True;}

Obviously, Java will try to call the ensurecapacity method regardless of whether the array size is sufficient. Although most of this method does not expand the Array (enough space), this causes an additional call of the method .. Net.

//. NetPublic VoidAdd (T item ){If(This. _ Size =This. _ Items. Length ){This. Ensurecapacity (This. _ SIZE +1);}This. _ Items [This. _ SIZE ++] =Item;This. _ Version ++;}

There seems to be different opinions on the scale-up,

 // Java       Public   Void Ensurecapacity ( Int  Mincapacity) {modcount ++ ;  Int Oldcapacity = Elementdata. length;  If (Mincapacity> Oldcapacity) {object olddata [] = Elementdata;  Int Newcapacity = (oldcapacity * 3)/2 + 1;  If (Newcapacity < Mincapacity) newcapacity = Mincapacity;  //  Mincapacity is usually close to size, so this is a win: Elementdata = Arrays. copyof (elementdata, newcapacity );}} 

Java uses a strange proportion, increasing the size by 2/3, And. Net seems very simple, increasing by 2 times. I still cannot judge who is more scientific.

 //  . Net  Private   Void Ensurecapacity (Int  Min ){  If ( This . _ Items. Length < Min ){  Int Num = ( This . _ Items. Length = 0 )? 4 :( This . _ Items. length * 2  );  If (Num> 2146435071 ) {Num = 2146435071  ;}  If (Num < Min) {num = Min ;}  This . Capacity = Num ;}} 
    • Add a batch of data
 //  Java  Public   Boolean Addall ( Int Index, collection <?Extends E> C ){  If (Index> size | index <0 )  Throw   New  Indexoutofboundsexception ( "Index:" + index + ", size:" + Size); object [] = C. toarray ();  Int Numnew = A. length; ensurecapacity (size + Numnew ); // Increments modcount        Int Nummoved = size- Index;  If (Nummoved> 0 ) System. arraycopy (elementdata, index, elementdata, Index + Numnew, nummoved); system. arraycopy (, 0 , Elementdata, index, numnew); Size + = Numnew;  Return Numnew! = 0 ;} 

 

When adding a batch of data, Java first copies the data (toarray) to the temporary array and then copies the data to the actual array elementdata.

//  . Net Icollection <t> collection2 = Collection As Icollection <t> ;  If (Collection2! = Null  ){  Int Count = Collection2.count;  If (Count> 0  ){  This . Ensurecapacity (This . _ SIZE + Count );  If (Index < This  . _ SIZE) {array. Copy (  This . _ Items, index, This . _ Items, index + count, This . _ Size- Index );}  If ( This = Collection2) {array. Copy ( This . _ Items, 0 , This  . _ Items, index, index); array. Copy (  This . _ Items, index + count, This . _ Items, Index * 2 , This . _ Size- Index );}  Else  {T [] Array = New T [count]; collection2.copyto (array,  0  ); Array. copyto (  This  . _ Items, index );}  This . _ SIZE + = Count ;}} 

. NetCodeIn theory, he can call the copyto of collection2 to directly store this. _ The specified location of items reduces the number of copies, but I don't know why he didn't.

    • Other differences

Java uses the equality method of objects, while. net uses the equality Calculation of generic versions, which is more efficient. When clear is used, the for loop is used to clear data content, while. Net calls the quick method of array.

Are there other differences? I haven't found it yet. I don't feel much difference in most cases. Maybe. Net is a little more meticulous. What do you think?

 

 

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.