Directorio artículo
recipiente recorrido
1, Lista de recorrido de los tres métodos
(. 1) para atravesar el bucle de lista
(2) mejorado para bucle itera lista
(. 3) Entero de recorrido de la lista de los iteradores
List<String> list=new ArrayList<String>();
list.add("111");
list.add("222");
list.add("333");
list.add("444");
//for循环遍历list
System.out.println("=====for循环遍历list=====");
for(int i=0;i<list.size();i++){
System.out.println(list.get(i));
}
//增强for循环遍历list
System.out.println("=====增强for循环遍历list=====");
for(String str:list){
System.out.println(str);
}
//迭代器Integer遍历list
System.out.println("=====迭代器Integer遍历list=====");
Iterator it=list.iterator();
while(it.hasNext()){
System.out.println(it.next());
}
2, Mapa atraviesan seis métodos
Muestra el código siguiente los seis métodos, pero de hecho, tres métodos map.keySet (), map.EntrySet (), y map.values (), respectivamente, y para el refuerzo de iterador iterador alcanzados. Véase en particular el código:
Map<Integer, String> map = new HashMap<Integer, String>();
map.put(1, "AAA");
map.put(2, "BBB");
map.put(3, "CCC");
// map.keySet()和for遍历key、value
System.out.println("=====map.keySet()遍历key、value=====");
for (Integer key : map.keySet()) {
System.out.println(key + "-----" + map.get(key));
}
// map.keySet()和Iterator遍历key、value
System.out.println("=====map.keySet()和Iterator遍历key、value=====");
Iterator<Integer> it = map.keySet().iterator();
while (it.hasNext()) {
Integer key = it.next();
System.out.println(key + "-----" + map.get(key));
}
// map.entrySet()和for遍历key、value
System.out.println("=====map.entrySet()和for遍历key、value=====");
for (Map.Entry<Integer, String> entry : map.entrySet()) {
System.out.println(entry.getKey() + "-----" + entry.getValue());
}
// map.entrySet()和Iterator遍历key、value
System.out.println("=====map.entrySet()和Iterator遍历key、value=====");
Iterator<Map.Entry<Integer, String>> it2 = map.entrySet().iterator();
while (it2.hasNext()) {
Map.Entry<Integer, String> entry = it2.next();
System.out.println(entry.getKey() + "-----" + entry.getValue());
}
// for:只遍历map中的value,不遍历key
System.out.println("=====for:只遍历map中的value,不遍历key=====");
for (String value : map.values()) {
System.out.println(value);
}
// Iterator:只遍历map中的value,不遍历key
System.out.println("=====Iterator:只遍历map中的value,不遍历key=====");
Iterator<String> it3 = map.values().iterator();
while (it3.hasNext()) {
System.out.println(it3.next());
}
3, los dos métodos Set de recorrido
Al recorrer el resumen lista de rostros y mapa, y no encontró nada más que mejorada para y iterador iterador para recorrer, lista también se puede utilizar para el bucle en general a través. Así método Conjunto de recorrido es de dos tipos. Ver Código:
Set<Integer> set=new HashSet<Integer>();
set.add(1);
set.add(2);
set.add(3);
//增强for循环遍历
for(Integer it:set){
System.out.println(it);
}
//迭代器iterator遍历
Iterator<Integer> it=set.iterator();
while(it.hasNext()){
System.out.println(it.next());
}
4, tres métodos de matriz traversal
int[] arr={1,6,8,3,2,9};
//for循环遍历
for(int i=0;i<arr.length;i++){
System.out.println(arr[i]);
}
//增强for遍历
for(int i:arr){
System.out.println(i);
}
//Arrays.toString(arr)遍历,将数组转化为字符串形式并输出
System.out.println(Arrays.toString(arr));