目录
一,数组的遍历方式
数组有两种遍历方式:
public class ArrayXunhuan {
public static void main(String[] args) {
int[] arr = new int[]{1,2,3,4,5}; // 或者 int[] arr1 = {1,2,3,4,5};
//数组循环(方法一)当然还有while和do...while循环,这都属于一类
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " "); // 1 2 3 4 5
}
System.out.println();
int i1 = 0;
while (i1 < arr.length) {
System.out.print(arr[i1] + " "); //1 2 3 4 5
i1 ++;
}
System.out.println();
int i2 = 0;
do {
System.out.print(arr[i2] + " "); // 1 2 3 4 5
i2 ++;
} while (i2 < arr.length);
System.out.println();
//数组循环(方法二)增强for循环
for (int i3 : arr) {
System.out.print(i3 + " "); // 1 2 3 4 5
}
}
}
二,List的遍历方式
List有三钟遍历方式:
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class ListXunhuan {
public static void main(String[] args) {
List<Integer> list = new ArrayList<Integer>();
list.add(1);
list.add(2);
list.add(3);
//list遍历(方法一)普通for循环
for (int i = 0; i < list.size(); i++) {
System.out.print(list.get(i) + " "); // 1 2 3
}
System.out.println();
//list遍历(方法二)增强for循环
for (Integer data : list) {
System.out.print(data + " "); // 1 2 3
}
System.out.println();
//list遍历(方法三)迭代器
Iterator<Integer> ite = list.iterator();
while (ite.hasNext()) {
Integer data = (Integer) ite.next();
System.out.print(data + " "); // 1 2 3
}
}
}
三,Set的遍历方式
Set有二钟遍历方式:
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
public class SetXunhuan {
public static void main(String[] args) {
Set<Integer> set = new HashSet<Integer>();
set.add(1);
set.add(4);
set.add(3);
System.out.println(set); // [1, 3, 4]
//set遍历(方法一)增强for循环
for (Integer data : set) {
System.out.print(data + " "); // 1 3 4
}
System.out.println();
//set遍历(方法二)迭代器
Iterator<Integer> ite = set.iterator();
while (ite.hasNext()) {
Integer data = (Integer) ite.next();
System.out.print(data + " "); // 1 3 4
}
}
}
四,Map的遍历方式
Map有三钟遍历方式:
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public class MapXunhuan {
public static void main(String[] args) {
Map<String, String> map = new HashMap<String,String>();
map.put("key3", "value3");
map.put("key1", "value1");
map.put("key2", "value2");
System.out.println(map); // {key3=value3, key2=value2, key1=value1}
// map遍历(方法一)
// 遍历key的值(keySet)
Set<String> set = map.keySet();
for (String str : set) {
System.out.print(str + " " + map.get(str) + "\t"); // key3 value3 key2 value2 key1 value1
}
System.out.println();
// 遍历value的值values
Collection<String> set1 = map.values(); // value3 value2 value1
for (String str : set1) {
System.out.print(str + " ");
}
System.out.println();
// map遍历(方法二)
Set<Map.Entry<String, String>> entry = map.entrySet();
for (Map.Entry<String, String> entry1 : entry) {
System.out.print(entry1.getKey() + " " + entry1.getValue() + "\t"); // key3 value3 key2 value2 key1 value1
}
System.out.println();
// map遍历(方法三)
Iterator<Map.Entry<String, String>> ite = map.entrySet().iterator();
while (ite.hasNext()) {
Map.Entry<String, String> entry2 = (Map.Entry<String, String>) ite.next();
System.out.print(entry2.getKey() + " " + entry2.getValue() + "\t"); // key3 value3 key2 value2 key1 value1
}
}
}
好了,大家可以自己动手来试试哦,欢迎留言点赞和收藏