Precautions for using remove in arraylist in Java

Source: Internet
Author: User

From: http://blog.sina.com.cn/s/blog_4914a33b0100xtyj.html

// When removing a message,
The size of arraylist has changed (that is, list. Size () has changed );
When I is greater than or equal to list. Size (), the loop jumps out, And the arraylist can no longer be executed;
So you must execute I --, that is, I = I-1;
Now we can traverse all the information in the list. You cannot use the remove feature in the iterator traversal. An exception is thrown. (Java. util. concurrentmodificationexception; At java. util. arraylist $ arraylistiterator. Next)

Public class trickyal {

 

 
    Publicstatic upload list <user> LlU = newclientlist <user> ();
    Publicstatic hashmap <string, user> hmu = newhashmap <string, user> ();
    Publicstatic arraylist <user> ulist = newarraylist <user> (arrays. aslist
    (Newuser [] {
           
            New User (2, "Eric "),
            New User (3, "Eric "),
            New User (1, "Aaron "),
            New User (4, "Eric ")
   
    }
    ));
      Static {
            LlU. Add (new user (1, "Aaron "));
            LlU. Add (new user (2, "Eric "));
            LlU. Add (new user (3, "Tom "));
            Hmu. Put ("1", new user (3, "Tom "));
            Hmu. Put ("2", new user (2, "Eric "));
            Hmu. Put ("3", new user (1, "Leo "));
            Hmu. Put ("3", new user (4, "Scott "));
           
           
    }
    Publicstatic voiddeleteuser (arraylist <user> urlist, string auser ){
           
            Iterator itr = urlist. iterator ();
            Int c = urlist. Size ();
           
            For (INT I = 0; I <C; I ++ ){
                  If (auser. Equals (urlist. Get (I). Name )){
                        Urlist. Remove (I );
                        I --;
                        C --;
                  }
            }
           
           
    }
Public static voiddeleteuser (partition list <user> urlist, string auser ){
           
            Iterator itr = urlist. iterator ();
            Int c = urlist. Size ();
           
     

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.