zoukankan      html  css  js  c++  java
  • JAVA集合遍历

    一.Set

    public class text {
        public static void main(String[] args) {
            Set<String> a = new HashSet<String>();
            a.add("aa");
            a.add("bb");
            a.add("cc");
            a.add("dd");
            a.add("ee");
            a.add("ff");
            // 1.使用foreach遍历
            for (Object i : a) {
                System.out.println(i);
            }
            // 2.使用迭代器iterator遍历
            // (1)遍历
            Iterator<String> it = a.iterator();
             while (it.hasNext()) {
             System.out.println(it.next());
             }
            // (2)移除某个元素
            Iterator<String> it2 = a.iterator();
            while (it2.hasNext()) {
                if (it2.next().equals("aa")) {
                    it2.remove();
                }
            }
            for (Object i : a) {
                System.out.println(i);
            }
        }
    }

    二.List

    public class text2 {

     public static void main(String[] args) {
      List<String> a = new ArrayList<String>();
      a.add("aa");
      a.add("bb");
      a.add("cc");
      a.add("dd");
      a.add("ee");
      a.add("ff");
      // 1.使用foreach遍历
      for (Object i : a) {
       System.out.println(i);
      }
      // 2.使用迭代器iterator遍历
      // (1)遍历
      Iterator<String> it = a.iterator();
      while (it.hasNext()) {
       System.out.println(it.next());
      }
      // (2)移除某个元素
      Iterator<String> it2 = a.iterator();
      while (it2.hasNext()) {
       if (it2.next().equals("aa")) {
        it2.remove();
       }
      }
      for (Object i : a) {
       System.out.println(i);
      }

     }

    }

  • 相关阅读:
    OSError: Initializing from file failed
    python之邮件提醒
    python之经纬度的获取
    Pandas写入CSV格式
    代码不同之处高亮显示
    Python之免费随机代理IP的获取以及使用
    正则之利用元素属性进行匹配
    时间戳的格式化
    简单实用的HTML中字符串的提取
    承接OpenCV Halcon视觉项目开发定制
  • 原文地址:https://www.cnblogs.com/zhangxin4477/p/7493695.html
Copyright © 2011-2022 走看看