Java Android makes successive, selective deletions of a collection

Source: Internet
Author: User

In our general code, if you want to delete a few items inside the collection, it is to traverse this collection, and then in the inside to determine whether it meets the deletion requirements, and then delete

The code is as follows:

for (int j=0;j<liststring.size (); j + +) {if (Booleanlist.get (j)) {Booleanlist.remove (j); Liststring.remove (j); System.out.println ("Delete data: Index:" +j+ "current collection Size:" +liststring.size ());}}
Such code, if all of our elements of this collection are symbol deletion criteria, then this collection can only delete half of the elements: Because I is increasing, and size is getting smaller


If we get the collection in advance, the action may go out of bounds.


The real way to do this is, in the collection every time we delete this element, if we remove it, then we will be the value of I minus 1, so that he still the next time from this position to calculate, to solve the problem

Originally thought can trouble. Really calm down, in fact, is also very good solution, so must calm down

Full code:

Package Test;import Java.util.arraylist;public class Testlistremove {public static void main (string[] args) {arraylist& Lt string> liststring = new arraylist<string> (); arraylist<boolean> booleanlist = new arraylist<boolean> (); SYSTEM.OUT.PRINTLN ("Initialize data"), for (int. i=1;i<5;i++) {Liststring.add ("This is index:" +i),//false true false true to delete 2nd 4th if (i%2==0) {Booleanlist.add (true);} else {Booleanlist.add (false);}} System.out.println ("delete data"), for (int j=0;j<liststring.size (); j + +) {if (Booleanlist.get (j)) {Booleanlist.remove (j) ; Liststring.remove (j);//If the current location of the data we need to delete, then we will delete, and then remember to reduce the cursor//so that he will still be removed from this location next time, because our deletion operation, can cause the back of the j--; System.out.println ("Delete data: Index:" +j+ "current collection Size:" +liststring.size ());}} copyonwritearraylist<string> copyonwritearraylist = new copyonwritearraylist<string> (listString);// Online said there is a way to use copyonwritearraylist, there is no attempt}}


Java Android makes successive, selective deletions of a collection

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.