Correct traversal of the element method in the delete list (recommended) _java

Source: Internet
Author: User
Tags concurrentmodificationexception

There are a number of ways to traverse the deletion of elements in a list, which can cause problems when used improperly. Here are some of the following ways to remove elements from a list:

1. Remove multiple elements that meet the criteria through an enhanced for loop

2. Delete an element that meets the criteria through an enhanced for loop

3. Remove multiple elements that match the criteria through a normal for deletion

4. Traversing through the iterator to remove multiple elements that meet the criteria

/**
   * Using an enhanced for loop
   * After removing elements from the list during the loop, the loop list will be reported concurrentmodificationexception/
   public
  void Listremove () {
    list<student> students = this.getstudents ();
    for (Student stu:students) {
      if (stu.getid () = = 2) 
        students.remove (stu);
    }
  
/**
   * Like this use an enhanced for loop to traverse the List to delete, but immediately after the deletion of the jump will not appear abnormal
  /public void Listremovebreak () {
    list< student> students = this.getstudents ();
    for (Student stu:students) {
      if (stu.getid () = = 2) {
        students.remove (stu);
        break;
      }}}
  
/**
   * This traversal may omit an element because the size of the list is changed by the deletion of the element, and
   the index of the element changes, for example, when you loop to the 2nd element you delete it, and
   then you go to the 3rd element, The 4th element that was actually accessed was the previous one. When the element * index that is accessed exceeds the size of the
   current list, there is also an exception that crosses the bounds of the array, but there is no such exception here,
   * Because the size of the current list is picked up once for each traversal.
   */public
  void ListRemove2 () {
    list<student> students = this.getstudents ();
    for (int i=0; i<students.size (); i++) {
      if (students.get (i). GetId ()%3 = 0) {
        Student Student = Students.get ( i);
        Students.remove (student);}}
  
/** * Using iterator can also be successfully deleted and traversed/public void Iteratorremove () {List<stud
    ent> students = this.getstudents ();
    SYSTEM.OUT.PRINTLN (students);
    iterator<student> stuiter = Students.iterator ();
      while (Stuiter.hasnext ()) {Student Student = Stuiter.next ();
        if (Student.getid ()% 2 = 0)//Here to remove the current object using the iterator Remove method, the same will occur if you use the Remove method of the list concurrentmodificationexception
    Stuiter.remove ();
  } System.out.println (students); }
Import java.util.ArrayList;
Import Java.util.Iterator;

Import java.util.List;
    public class Listremove {public static void main (String args[]) {listremove lr = new Listremove ();
    Lr.listremove ();
Lr.listremovebreak ();
Lr.listremove2 ();
  Lr.iteratorremove (); /** * Using an enhanced for loop * After removing elements from the list during a loop, the concurrentmodificationexception/public void is reported when the loop list is continued listremove
    () {list<student> students = this.getstudents ();
    for (Student stu:students) {if (Stu.getid () = = 2) students.remove (STU); }/** * Like this use an enhanced for loop to traverse the List to delete, but immediately after the deletion of the jump will not appear abnormal * * public void Listremovebreak () {List<studen
    t> students = this.getstudents ();
        for (Student stu:students) {if (Stu.getid () = = 2) {students.remove (STU);
      Break
   }}/** * This does not use an enhanced for loop, does not error when the size traversal of the list is retrieved, but the result may be incorrect. */public void ListRemove2 () {list<student> students = this. getstudents ();
    for (int i=0; i<students.size (); i++) {if (Students.get (i). GetId ()%2 = 0) students.remove (i); }/** * Using iterator can also be successfully deleted and traversed * * public void Iteratorremove () {list<student> students = this
    . getstudents ();
    SYSTEM.OUT.PRINTLN (students);
    iterator<student> stuiter = Students.iterator ();
      while (Stuiter.hasnext ()) {Student Student = Stuiter.next ();
    if (Student.getid ()% 2 = 0) stuiter.remove ();
  } System.out.println (students);
      Private List<student> getstudents () {list<student> students = new arraylist<student> () {
        {int i = 0;
          while (i++ <) {Student Student = new Student (i, "201200" + I, "name_" + i);
        This.add (student);
    }
      }
    };
  return students; }
}
public class Student {

  private int id;
  Private String Stuno;
  private String name;
  
  Public Student () {
    
  } public
  
  Student (int id, string Stuno, string name) {
    this.id = ID;
    This.stuno = Stuno;
    this.name = name;

  public int getId () {return
    ID;
  }

  public void setId (int id) {
    this.id = ID;
  }

  Public String Getstuno () {return
    Stuno;
  }

  public void Setstuno (String stuno) {
    This.stuno = Stuno;
  }

  Public String GetName () {return
    name;
  }

  public void SetName (String name) {
    this.name = name;
  }

  @Override public
  String toString () {return
    "Student [id= + ID +", name= "+ name +", stuno= "+ Stuno
        +" ]";
  }
  
}

This is the correct way to traverse the deletion list of elements (recommended) is a small series to share all the content, hope to give you a reference, but also hope that we support the cloud-dwelling community.

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.