Enumeration (interface)
- 사전적 의미로 열거를 말함
Enumeration 인터페이스를 구현한 클래스들은 모두 아래 두 함수 구현체를 갖게 됨.
- boolean hasMoreElements()
- E nextElement()
hasMoreElements() 함수로 다음 읽을 항목이 더 있는지 알 수 있다.
nextElement() 함수로 다음 항목을 얻을 수 있다.
사용예:
package com.enumeration;
import java.util.Enumeration;
import java.util.Hashtable;
public class EnumerationTest {
public static void main(String[] args) throws Exception {
Hashtable table = new Hashtable();
table.put("name", "John");
table.put("age", "24");
table.put("weight", "60kg");
Enumeration keys = table.keys();
while (keys.hasMoreElements()) {
String key = keys.nextElement();
System.out.printf("%s : %s\n", key, table.get(key));
}
}
}
댓글 없음:
댓글 쓰기