Three methods of traversing ArrayList
Step 1: loop through with for
Through the previous learning, we know that we can use size() and get() to get the size, and get the elements of the specified location, and we can traverse the contents of ArrayList by combining with for loop
package collection; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import charactor.Hero; public class TestCollection { public static void main(String[] args) { List<Hero> heros = new ArrayList<Hero>(); // Put 5 heros into the container for (int i = 0; i < 5; i++) { heros.add(new Hero("hero name " + i)); } // The first traversal for loop System.out.println("--------for loop-------"); for (int i = 0; i < heros.size(); i++) { Hero h = heros.get(i); System.out.println(h); } } }
Step 2: iterator traversal
Traversing elements in a collection using Iterator
package collection; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import charactor.Hero; public class TestCollection { public static void main(String[] args) { List<Hero> heros = new ArrayList<Hero>(); //Put 5 heros into the container for (int i = 0; i < 5; i++) { heros.add(new Hero("hero name " +i)); } //The second kind of traversal, using iterators System.out.println("--------Use while Of iterator-------"); Iterator<Hero> it= heros.iterator(); //Judge whether "next" position has data from the first position //If there is one, take it out through next, and move the pointer down //Until "next" location has no data while(it.hasNext()){ Hero h = it.next(); System.out.println(h); } //for writing of iterator System.out.println("--------Use for Of iterator-------"); for (Iterator<Hero> iterator = heros.iterator(); iterator.hasNext();) { Hero hero = (Hero) iterator.next(); System.out.println(hero); } } }
Step 3: loop with enhanced for
Using enhanced for loop can easily traverse the elements in ArrayList, which is the first choice for many developers.
However, the enhanced for loop has some disadvantages:
Unable to initialize ArrayList
We can't know how many elements are currently. When we need to print only singular elements, we can't You must customize the subscript variable again.
package collection; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import charactor.Hero; public class TestCollection { public static void main(String[] args) { List<Hero> heros = new ArrayList<Hero>(); // Put 5 heros into the container for (int i = 0; i < 5; i++) { heros.add(new Hero("hero name " + i)); } // Third, enhanced for loop System.out.println("--------Enhancement type for loop-------"); for (Hero h : heros) { System.out.println(h); } } }
Practice: Delete data in ArrayList
First, initialize a Hero collection, which contains 100 Hero objects whose names are respectively from the
hero 0
hero 1
hero 2
...
hero 99.
By means of traversal, delete the object whose name number is multiple of 8
Answer:
package collection; import java.util.ArrayList; import java.util.List; import charactor.Hero; public class TestCollection { public static void main(String[] args) { List<Hero> heros = new ArrayList<Hero>(); // Put 100 heros into the container for (int i = 0; i < 100; i++) { heros.add(new Hero("hero name " + i)); } //Prepare a container for the objects to be deleted List<Hero> deletingHeros = new ArrayList<>(); for (Hero h : heros) { int id = Integer.parseInt(h.name.substring(10)); if (0 == id % 8) deletingHeros.add(h); } for (Hero h : deletingHeros) { heros.remove(h); } // heros.removeAll(deletingHeros); / / delete multiple Hero objects directly through removeAll System.out.println(heros); } }
It is not allowed to delete data while traversing data with Iterator and enhanced for loop. Otherwise, ConcurrentModificationException will be thrown
The solution is to prepare a temporary container for saving the objects to be deleted And then delete it