import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

public class ForTest {

    public static void main(String[] args) {

        // list集合的遍历
        List list = new ArrayList<>();
        list.add("a");
        list.add("b");
        list.add("c");
        for (int i = 0; i < list.size(); i++) {
            System.out.print(list.get(i));
        }
        System.out.println("");
        Iterator it = list.iterator();
        while (it.hasNext()) {
            System.out.print(it.next());
        }
        System.out.println("");
        for (Iterator iterator = list.iterator(); iterator.hasNext();) {
            System.out.print(iterator.next());
        }
        System.out.println("");
        // map集合的遍历
        Map map = new HashMap<>();
        map.put("1", "苹果");
        map.put("2", "香蕉");
        map.put("3", "柠檬");
        // for循环遍历
        for (int j = 1; j <= map.size(); j++) {
            String num = String.valueOf(j);
            System.out.print(map.get(num));
        }
        System.out.println();
        // 通过获取所有的key按照key来遍历
        Set set = map.keySet();
        for (Object iString : map.keySet()) {
            System.out.print(map.get(iString));
        }
        System.out.println();
        // 通过entrySet遍历
        Iterator iterator = map.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry<String, String> entry = (Entry<String, String>) iterator.next();
            System.out.println("键:" + entry.getKey() + "值:" + entry.getValue());
        }
    }

}

List遍历方式,Map的遍历方式

相关文章:

  • 2021-09-20
  • 2021-11-30
  • 2021-11-30
  • 2021-11-30
  • 2021-11-24
  • 2021-10-07
  • 2021-11-04
  • 2021-11-04
猜你喜欢
  • 2021-09-20
  • 2021-12-31
  • 2021-11-30
  • 2021-09-20
  • 2021-09-20
  • 2021-12-10
  • 2021-09-20
相关资源
相似解决方案