1. Use an iterator to traverse the ArrayList collection
Package com.jredu.oopch07;
Import java.util.ArrayList;
Import java.util.Iterator;
Import java.util.List;
Public class Ch05 {
Public static void main(String[] args) {
// TODO Auto-generated method stub
List list = new ArrayList<>();
//collection
List.add(1);
List.add(2);
List.add(3);
//Iterator iterator
//1, get an iterator
Iterator iter = list.iterator();
//2, iterating through the loop
//hasNext(): determine if there is a next element
While(iter.hasNext()){
//If it exists, call next to iterate
//Object-->Integer-->int
Int j=(int)iter.next(); //Strengthen Object to int
System.out.println(j);
}
}
}
2, use the iterator to traverse the Set collection
Package com.jredu.oopch08;
Import java.util.HashSet;
Import java.util.Iterator;
Import java.util.Set;
Public class Ch01 {
Public static void main(String[] args) {
/ / Store the address of the data
Set set = new HashSet<>();
//Storing data
Set.add(new Theme(1,"Title 1","Introduction 1"));
Set.add(new Theme(2,"Title 2","Introduction 1"));
/ / Traverse the data
Iterator iter = set.iterator();
While(iter.hasNext()){
Theme theme = (Theme)iter.next();
System.out.println(theme.getId()+theme.getTitle()+theme.getRemark());
}
}
}
3, use iterator to traverse the Map collection
Package com.jredu.oopch08;
Import java.util.Collection;
Import java.util.HashMap;
Import java.util.Iterator;
Import java.util.Map;
Import java.util.Set;
Public class Ch03 {
Public static void main(String[] args) {
// TODO Auto-generated method stub
Map map = new HashMap<>();
Map.put(1, "a");
Map.put(2, "b");
Map.put(3, "c");
//All keys in a key-value pair form a collection
Set set = map.keySet();
Iterator iter = set.iterator();
While(iter.hasNext()){
System.out.println(iter.next());//Print out the keys in the map (1,2,3)
}
/ / Print out the value
//values a collection of all the values
Collection col = map.values();
/ / Rewritten the toString method
System.out.println(col);//Print out the values of a, b, c
}
}
Package com.jredu.oopch08;
Import java.util.Collection;
Import java.util.HashMap;
Import java.util.Iterator;
Import java.util.Map;
Import java.util.Set;
Public class Ch04 {
Public static void main(String[] args) {
Map map=new HashMap<>();
Map.put(1, "a");
Map.put(2, "b");
Map.put(3, "c");
/ / Must master
//All keys in a key-value pair are grouped into a set set
Set set=map.keySet();
System.out.println(set);
//values a collection of all the values
Collection col=map.values();
System.out.println(col);
/ / Get all the keys and values
//entrySet can get a collection of all key-value pairs
//The inside is all the data (key-value) stored.
Set<Map.Entry<Integer, String>> entrySet=map.entrySet();
Iterator<Map.Entry<Integer, String>> iter=entrySet.iterator();
While(iter.hasNext()) {
Map.Entry<Integer, String> entry=iter.next();
System.out.println("key:"+entry.getKey());
System.out.println("value:"+entry.getValue());
}
// Iterator iter=col.iterator();
// while(iter.hasNext()) {
// System.out.println(iter.next());
// }
// Iterator iter=set.iterator();
// while(iter.hasNext()) {
// System.out.println(iter.next());
// }
// System.out.println(map);
}
}