zoukankan      html  css  js  c++  java
  • Java13-Iterator的应用

    一、Iterator常用的方法
    • hasNext()检查集合中是否还有元素,返回布尔值
    • 使用next()获得集合中的下一个元素

      二、迭代器的应用

    package doudou;
    
    import java.util.ArrayList;
    import java.util.Collection;
    import java.util.Iterator;
    import java.util.List;
    
    public class test_Iterator {
    
        public static void main(String[] args) {
            System.out.println("================for循环======================");
            int[] a = { 1, 2, 3, 4, 2, 1 };
            for (int i = 0; i < a.length; i++) {
                 System.out.println(a[i]);
    
            }
            System.out.println("=================iterator迭代-遍历====================");
            List<String> arr = new ArrayList<String>();
            arr.add("a");
            arr.add("b");
            arr.add("c");
            arr.add("d");
            arr.add("e");
            arr.add("f");
            Iterator<String> iter = arr.iterator();
            // iterator.hasNext()如果存在元素的话返回true
            while (iter.hasNext()) {
                // iterator.next()返回迭代的下一个元素
                System.out.print(iter.next() + "、");
    //            if (iter.next() == "f") {
    //                System.exit(0);
    //            }
           
                 if(iter.next().equals("f")){
                 System.out.println("you get it");
                 }
            }
            
            System.out.println("=================iterator迭代-遍历=====================");
            Collection c=new ArrayList<Students>();
            c.add(new Students("张三", "23", "女"));
            c.add(new Students("李四", "3", "女"));
            c.add(new Students("张私", "2", "男"));
            c.add(new Students("流三", "13", "女"));
            System.out.println(c);
            @SuppressWarnings("rawtypes")
            Iterator<Students>  it =c.iterator();
            while(it.hasNext()) {
                Students stu =it.next();
                System.out.println(stu.getName()+" "+stu.getAge()+" "+stu.getSex());
            }
        }
    }

    Students类

    package doudou;
    
    public class Students {
        private String name;
        private String age;
        private String sex;
        
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public String getAge() {
            return age;
        }
        public void setAge(String age) {
            this.age = age;
        }
        public String getSex() {
            return sex;
        }
        public void setSex(String sex) {
            this.sex = sex;
        }
        public Students(String name, String age, String sex) {
            super();
            this.name = name;
            this.age = age;
            this.sex = sex;
        }
    
        
        
    
    }
  • 相关阅读:
    Codeforces_731_C
    Codeforces_731_B
    Codeforces_731_A
    HDU_1035_水
    POJ_3450_KMP
    POJ_2185_二维KMP
    POJ_2752_KMP
    Codeforces_492_E
    Codeforces_338_D
    Codeforces_327_C
  • 原文地址:https://www.cnblogs.com/eosclover/p/13560809.html
Copyright © 2011-2022 走看看