프로그래밍/Java

Vector, HashTable, Enumeration 예제

꿈풀이 2014. 11. 3. 13:17

public class Exam01 {


 public static void main(String[] args) {


  Vector<String> v = new Vector<String>(); // Vector 선언

  v.addElement(new String("망아지"));// Vector에 데이타 삽입
  v.addElement(new String("송아지"));
  v.addElement(new String("강아지"));
  v.addElement(new String("병아리"));

  Hashtable<String, String> h = new Hashtable<String, String>(); // Hashtable
                  // 선언

  h.put("1", new String("홍길동")); // Hashtable에 데이타 삽입
  h.put("2", new String("안녕하세요"));
  h.put("3", new String("02-111-1111"));
  h.put("4", new String("010-111-1111"));

  Enumeration<String> ev = v.elements(); // Enumeration 선언 Enumeration 은
  // Vector나 Hashtable에 자료가 있을 경우 선언된다.

  while (ev.hasMoreElements()) {// hasMoreElements => 개체가 있을떄까지 반복~
   String temp = ev.nextElement(); // 임시 함수 temp 에 개체를 대입한다.while문
           // 반복될떄마다 Next 됨
   System.out.println("벡터의 Enumeration : " + temp);
   ;// 출력
  }// while

  System.out.println();

  Enumeration<String> ei = h.elements();// 이번에는 Hashtable

  while (ei.hasMoreElements()) {
   String temp = ei.nextElement();
   System.out.println("해시의 Enumeration : " + temp);
  }// while

  System.out.println();

  Iterator<String> ih = h.values().iterator(); // 만약 객체가 들어 있는 Hashtable
              // h가 있다면 할당

  while (ih.hasNext()) {
   String temp = ih.next();
   System.out.println("해시의 Iterator : " + temp);


  }// while

 }// main

}