package ibm; import java.util.*; class PrintData{ static void print(Enumeration e){ while(e.hasMoreElements()){ System.out.println(e.nextElement().toString());//toString方法重载过 } } } class Mouse{ int mouseNumber; Mouse(int i){ this.mouseNumber=i;} public String toString(){ return "this is a mouse "+mouseNumber; } } class Hasmeter{ int hasNumber; Hasmeter(int i){ this.hasNumber=i;} public String toString(){ return "this is a hasmeter "+hasNumber; } } public class HashTest2 { public static void main(String[] args) { HashTest2 hashtest2 = new HashTest2(); Vector vc=new Vector(); for (int i=0;i<5;i++) {vc.addElement(new Mouse(i));} Hashtable ht=new Hashtable(); for (int i=0;i<5;i++){ht.put(new Integer(i),new Hasmeter(i));}//放入堆 if(ht.containsKey(3)){System.out.println(ht.get(3));} System.out.print(ht+"\n"); PrintData.print(vc.elements()); PrintData.print(ht.elements()); } } 结果如下: this is a hasmeter 3 {4=this is a hasmeter 4, 3=this is a hasmeter 3, 2=this is a hasmeter 2, 1=this is a hasmeter 1, 0=this is a hasmeter 0} this is a mouse 0 this is a mouse 1 this is a mouse 2 this is a mouse 3 this is a mouse 4 this is a hasmeter 4 this is a hasmeter 3 this is a hasmeter 2 this is a hasmeter 1 this is a hasmeter 0 package ibm; import java.util.*; public class IreratorTest { public static void main(String[] args) { IreratorTest ireratortest = new IreratorTest(); Collection c=new ArrayList(); for(int i=1;i<=100;i++){ c.add(Integer.toString(i)); } Iterator it=c.iterator(); while(it.hasNext()){ System.out.println(it.next()); } } }
java enumeration 创建_一个java Enumeration类型的使用方法
最新推荐文章于 2021-04-01 16:47:08 发布