A set of frame types traversal -3

Traversal

public class Person {
    private String name;
    private int age;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }
}
public static void main(String[] args){
	ArrayList<Person>arr=new ArrayList<>();
	arr.add(new Person("Jim",12));
	arr.add(new Person("Tom",14));
	arr.add(new Person("Jack",13));
	//分别调用下列各方法

Ordinary for loop iterates

public static void travering1(ArrayList<Person> arr){
	for(int i=0;i<arr.size();i++){
		System.out.println(arr.get(i).getName()+" "+arr.get(i).getAge());
	}
}

Here Insert Picture Description

//在遍历的同时增加或删除原数据元素
public static void travering_change1(ArrayList<Person> arr){
        for(int i=0;i<arr.size();i++){
            System.out.println(arr.get(i).getName()+" "+arr.get(i).getAge());
            if(arr.get(i).getAge()==12){
            //arr.get(i).setAge(22);
           //   arr.add(new Person("ss",88));
           arr.remove(arr.get(i));
            }
        }
    }

Delete
Here Insert Picture Description
increase
Here Insert Picture Description
modification
Here Insert Picture Description

Enhanced for loop

public static void travering2(ArrayList<Person> arr){
	for(Person p:arr){
		System.out.println(p.getName()+" "+p.getAge());
	}
}

Here Insert Picture Description

//在遍历的同时增加或删除原数据元素
    public static void travering_change2(ArrayList<Person> arr){
        for(Person p:arr){
            if(p.getAge()==13){
            //p.setAge(33);
              // arr.add(new Person("pp",45));
                arr.remove(p);
            }
            System.out.println(p.getName()+" "+p.getAge());
        }
    }

Delete
Here Insert Picture Description
increase
Here Insert Picture Description
modification
Here Insert Picture Description

Iterator loop

public static void travering3(ArrayList<Person> arr){
	Iterator<Person> it=arr.iterator();
	while(it.hasNext()){
		Person p=it.next();
		System.out.println(p.getName()+" "+p.getAge());
	}
}

Here Insert Picture Description

//在遍历的同时增加或删除原数据元素
    public static void travering_change3(ArrayList<Person> arr){
        Iterator<Person> it=arr.iterator();
        while(it.hasNext()){
            Person p=it.next();
            if(p.getAge()==13){
                //p.setAge(33);
               // arr.add(new Person("hh",33));
                arr.remove(p);
            }
            System.out.println(p.getName()+" "+p.getAge());
        }
    }

Delete
Here Insert Picture Description
increase
Here Insert Picture Description
modification
Here Insert Picture Description
from the above it can be seen: for the three traversal time for additions and deletions to change the operation of three ways, you can only modify the operating output correctly, and for additions and deletions will be reported at run time ConcurrentModificationException exception. This is because in an iterative process to a set of elements of a method of operating, in iteration change set does not know the heavy elements, easily lead to uncertainty in the data. Solution can retrieve an iterator. When using three typically requires traversal deletions so as not to select the enhanced for loop iterates (bottom iterators) and iterates over operation.

Guess you like

Origin blog.csdn.net/qq_44693194/article/details/92377032