What to look out for when you delete JAVA list

Source: Internet
Author: User

When the list of Java is deleted, List.remove (O) is generally used; But this tends to cause problems, first look at the following code:

Package Com.demo;import Java.util.arraylist;import Java.util.list;public class Test11 {public void delete () {list< integer> list = new arraylist<integer> (); List.add (1); List.add (2); List.add (2); List.add (3); for (int i = 0; i < List.size (); i++) {if (List.get (i) ==2) {list.remove (i);}} This.outputlist (list);} private void Outputlist (List<integer> List) {for (Integer i:list) {System.out.println (i);}} public static void Main (string[] args) {Test11 t = new Test11 (); T.delete ();}}

The return result is:

1

2

3

This is obviously not our expectation, we want to delete all the elements in the list is 2, but the output is 2, this is because at I equals 1 o'clock, the list of index 1 is deleted element 2, this time the list is [], but then, I increment, equal to 2, At List.get (i), the result is 3, that is, with the deletion of the list element, index is changed, this is the trap, so we have to look for a delete, the index does not change the way the iteration to delete, and iterator is After being created, a single-chain index table that points to the original object is established, and when the original number of objects changes , the contents of the index table are not synchronized, that is, the cursor is used to maintain the index table, so it can be deleted:

Package Com.demo;import Java.util.arraylist;import Java.util.iterator;import Java.util.list;public class Test11 { public void Delete () {list<integer> List = new arraylist<integer> (); List.add (1); List.add (2); List.add (2); List.add (3); This.iteratordelete (List.iterator (), 2); this.outputlist (list);} private void Iteratordelete (iterator<integer> it, int deleteobject) {while (It.hasnext ()) {int i = It.next (); if (i== DeleteObject) {it.remove ();}}} private void Outputlist (List<integer> List) {for (Integer i:list) {System.out.println (i);}} public static void Main (string[] args) {Test11 t = new Test11 (); T.delete ();}}

The result of this code is correct!

Some might say that I deleted it in iterator, why is the value of list changed? This question, self-thinking go! Can't think out, can change careers!

What to look out for when you delete JAVA list

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.