容器中的迭代方法Iterator

来源:互联网 发布:网络歌手男伤感歌曲 编辑:程序博客网 时间:2024/04/27 23:16
import java.util.*;class Name{    private String firstName, lastName;    Name(String first,String last){        firstName = first;        lastName = last;    }    String getFirst(){        return firstName;    }    String getLast(){        return lastName;    }    public String toString(){        return firstName + " " + lastName;    }    /*public boolean equals(Object obj){    }*/}public class TestIterato {    public static void main(String[] args) {        Collection col = new HashSet();        //这里是一段哈希组合        //特点是,无序,不重复,因为以近似内存地址的方式排序        col.add(new Name("F1","L1"));        col.add(new Name("F2","L2"));        col.add(new Name("F3","L3"));        Iterator cursor = col.iterator();        while(cursor.hasNext()){            Name na = (Name) cursor.next();            System.out.println                       " \t Last Name:" + na.getLast() +                        "\t First Name:" + na.getFirst());            if(na.getLast().equals("L2")){                cursor.remove();            }//游标区域,游标删除        }        System.out.println("The survive String are:" + col.toString());//这里是没有Name na的生命周期的。col作为容器是不能访问Name中的FirstName和LastName。    }}

本博客尝试用新的方法编写,感觉,不!咋!地!但是国外很流行这个。

0 0
原创粉丝点击