The difference between add and AddAll in Java ArrayList

Source: Internet
Author: User
Tags addall

We often have the need to put a list of data into the other list, the most stupid way of course is to add a traversal, but Java provides a AddAll method.

Unlike the Add method, the Add method is to add an item, regardless of what type of data you are, and put it in item as a data add. The AddAll is the same type of data, the existing list is spliced (may not be rigorous). As an example:

There are two lists below, and I want to add the data from the second list to the first list, which is added with the Add method:

for (String item:list2) {

List1.add (item);

}

If you use AddAll:

List1.addall (LIST2);

Isn't it convenient? Of course you can also define the location to add, you can use AddAll (int index, collection<?>);

The difference between add and AddAll in Java ArrayList

Related Article

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.