Java Arraylist.addall Method---arraycopy

Source: Internet
Author: User

The problem is this, a friend on the Internet sent an interview question:

There's 1 2 3 in Lista.


There's 4 5 6 in Listb.


Let Lista become 1 2 3 4 5 6

I think it's very simple, just say the For loop traversal to add just fine. As a result, the interviewer said

The answer is to use the AddAll method because it is efficient:

I said it was very confusing and I consulted the information.

The conclusion is as follows: in small amount of data, for loop efficiency is high, the AddAll method is efficient when large data quantity is used:

The reasons are as follows:

The implementation of ArrayList's AddAll is:

Public   boolean   addall (Collection   c)   { 
                         object[]   a   =   c.toarray (); 
                         int   numnew   =   a.length; 
Ensurecapacity (size   +   numnew);   increments   modcount 
                         system.arraycopy (A,   0,   elementdata,   size,   numnew); 
                         Size   + =   numnew; 
return   numnew   !=   0; 
             } 

Well, obviously. The array is copied using the


Arraycopy this method. This approach is to use the copy memory approach, more efficient than traversing the array block.

First locate the data source and then copy the memory of the segment.

It's worth noting, of course, that this function also uses the ToArray method, which is to traverse the operation

But if you need to traverse it multiple times, then AddAll will gain an advantage in performance. .

The following is a test on the web in 20 sets of data or for the high efficiency, but in the large amount of data when the Arraycopy method has a clear advantage.
http://www.exam8.com/computer/djks/dj2/Java/ziliao/200810/1314435.htmlhttp://www.exam8.com/computer/djks/dj2/ Java/ziliao/200810/1314435.html

Other than that:

The definition of arraycopy is this

public static native void Arraycopy (object src, int srcpos, object dest, int destpos,int length);

The native keyword means that the source code for this function is not in the JDK. But he's calling a function on the local computer.

This function is written in C or C + + and compiled into a DLL. Java call. So the efficiency is more than the for loop to block.

To sum up: why the use of AddAll method in large amount of data is efficient.

1. Use memory copy to move data.

2. Local function, high execution efficiency.

What about the small amount of data.

The above 2 points are not obvious, and first to call the ToArray method, in the small amount of data, the effect is not good for.

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.