Java by API/java.util/Enumeration

Материал из Java эксперт
Перейти к: навигация, поиск

Enumeration: hasMoreElements()

 
import java.util.Date;
import java.util.Enumeration;
import java.util.Hashtable;
public class Main{
  public static void main(String[] args) {
    Hashtable h = new Hashtable(20);
    System.out.println(h.put("one", new Integer(1)));
    System.out.println(h.put("name", "A"));
    System.out.println(h.put("date", new Date()));
    System.out.println(h.put("one", new Integer(4)));
    Enumeration e = h.keys();
    while (e.hasMoreElements())
      System.out.println(e.nextElement());
    e = h.elements();
    while (e.hasMoreElements())
      System.out.println(e.nextElement());
  }
}





Enumeration: nextElement()

 
import java.util.Date;
import java.util.Enumeration;
import java.util.Hashtable;
public class Main{
  public static void main(String[] args) {
    Hashtable h = new Hashtable(20);
    System.out.println(h.put("one", new Integer(1)));
    System.out.println(h.put("name", "A"));
    System.out.println(h.put("date", new Date()));
    System.out.println(h.put("one", new Integer(4)));
    Enumeration e = h.keys();
    while (e.hasMoreElements())
      System.out.println(e.nextElement());
    e = h.elements();
    while (e.hasMoreElements())
      System.out.println(e.nextElement());
  }
}





implements Enumeration

 
/*
 * Output:
1
2
3
4
5
 * 
  
 */
import java.util.Enumeration;
class collection implements Enumeration {
  private int count = 0;
  private boolean more = true;
  public boolean hasMoreElements() {
    return more;
  }
  public Object nextElement() {
    count++;
    if (count > 4)
      more = false;
    return new Integer(count);
  }
}
public class MainClass {
  public static void main(String args[]) {
    Enumeration e = new collection();
    while (e.hasMoreElements()) {
      System.out.println(e.nextElement());
    }
  }
}