Java-Replication ArrayList

Source: Internet
Author: User

Sometimes when we need to replicate ArrayList, the quickest way is to create a new instance, in fact each collection class has a corresponding construction method that is copied by itself. The Clone () method itself is defective and is not recommended for use. The recommended practices are as follows:

list<double> original =//some listlist<double> copy = new arraylist<double> (original);

The problem is found when you do the HBase project, and when scanner gets the next result, if you add the result directly to the result set and re-use the ArrayList, the final result set will be the last result.

The Modified code:

list<cell> curvals = new arraylist<cell> (); list<list<cell>> results = new arraylist<list<cell>> (); Boolean finish = False;do {Curvals.clea    R ();    finish = Scanner.next (curvals); list<cell> tmp = new arraylist<cell> (curvals); Copy intermediate result Results.add (TMP);} while (finish);

Article original link: http://stackoverflow.com/questions/715650/how-to-clone-arraylist-and-also-clone-its-contents

Java-Replication ArrayList

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.