1.四种遍历ArrayList的方法
package cn.itcast.chapter0607.foreach;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
public class Example {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
list.add("1");
list.add("2");
list.add("3");
list.add("4");
list.add("5");
Date begin = new Date();
for(int i=0;i<list.size();i++){
String temp = (String) list.get(i);
System.out.println(temp);
}
for(String temp:list){
System.out.println(temp);
}
for(Iterator<String> it=list.iterator();it.hasNext();){
String temp = it.next();
System.out.println(temp);
}
Iterator it = list.iterator();
while(it.hasNext()){
Object obj = it.next();
it.remove();
System.out.println(obj);
}
}
}
2.遍历Set的两种方法
import java.util.HashSet;
import java.util.Iterator;
class Student{
String name;
String id;
public Student(String id, String name){
this.id = id;
this.name = name;
}
public String toString(){
return id+":"+name;
}
}
public class Example {
public static void main(String[] args) {
HashSet hs = new HashSet();
Student stu1=new Student ("1","DaBao");
Student stu2 = new Student("3","XiaoBao");
Student stu3 = new Student ("3","XiaoBao");
hs.add(stu1);
hs.add(stu2);
hs.add(stu3);
for(Object obj:hs){
System.out.println(obj);
}
for(Iterator<Object> it =hs.iterator();it.hasNext();){
Object obj = it.next();
System.out.println(obj);
}
}
}
3.遍历Map
1.根据key获得value
package cn.itcast.chapter0610.HashMap01;
import java.util.HashMap;
import java.util.Map;
public class Example9 {
public static void main(String[] args) {
Map map = new HashMap();
map.put(1, "abc");
map.put("2", "Tom");
map.put("3", "java");
map.put("3", "asd");
System.out.println("1:"+map.get(1));
System.out.println("2:"+map.get("2"));
System.out.println("3:"+map.get("3"));
}
}
2.通过键查找值方式遍历
package cn.itcast.chapter0610.HashMap02;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public class Example {
public static void main(String[] args) {
Map map = new HashMap();
map.put("1", "asd");
map.put("2","Rose");
map.put("3", "Lucy");
Set keySet = map.keySet();
Iterator it = keySet.iterator();
while(it.hasNext()){
Object key = it.next();
Object value = map.get(key);
System.out.println(key+":"+value);
}
}
}
3.通过entrySet方法
public class Example12 {
public static void main(String[] args) {
Map map = new HashMap();
map.put("1", "Jack");
map.put("2", "tom");
map.put("3", "janf");
Set entrySet = map.entrySet();
Iterator it = entrySet.iterator();
while(it.hasNext()){
Map.Entry entry = (Map.Entry)it.next();
System.out.println("key:"+entry.getKey());
System.out.println("value:"+entry.getValue());
}
}
}
4.获得Map集合中所有值的value
public class Example13 {
public static void main(String[] args) {
Map map = new HashMap();
map.put("1", "Jack");
map.put("2", "tom");
map.put("3", "janf");
Collection values = map.values();
Iterator it = values.iterator();
while(it.hasNext()){
Object value = it.next();
System.out.println(value);
}
System.out.println("value:"+map.values());
}
}