'정렬'에 해당되는 글 1건

  1. 2009/11/06 Bywoong Collection으로 Iterator를 정렬합니다. (1)
[알림] 삭제된 동영상 및 이미지나 깨진 링크, 저작권에 문제가 될 소지가 있는 내용은 이곳에 알려주시면 바로 조치하도록 하겠습니다. 감사합니다. - Fortune Cookie
import java.util.*;

public class HashtableTest {

static String[] array1 = {"C","B","A"};
static String[] array2 = {"1","2","3"};

public static void main(String args[]) {

Hashtable h = new Hashtable();
h.put(array1[0], array2[0]);
h.put(array1[1], array2[1]);
h.put(array1[2], array2[2]);

// unsorted keys output
Iterator it = h.keySet().iterator();
while (it.hasNext()) {
String element = (String)it.next();
System.out.println(element+" "+(String)h.get(element));
}

System.out.println("============");

// sorted keys output
Vector v = new Vector(h.keySet());
Collections.sort(v);
it = v.iterator();
while (it.hasNext()) {
String element = (String)it.next();
System.out.println( element+" "+(String)h.get(element));
}
/*
output :
A 3
C 1
B 2
============
A 3
B 2
C 1
*/
}
}


Collection의 sort()를 이용해서 Iterator를 정렬합니다.
2009/11/06 16:52 2009/11/06 16:52
관련글타래

    댓글을 달아 주세요

    1. 아름드리님의 생각 2009/11/08 21:45  댓글주소  수정/삭제  댓글쓰기

      Hashtable이 아닌 ArrayList를 정렬하는 것도 혹시 있을까요?

    [로그인][오픈아이디란?]

    구독안내 주 2~3회 새글이 올라옵니다. 블로그 방문없이 업데이트 되는 글을 구독하세요. RSS . E-Mail . HanRSS . WZD . Google Reader . Bloglines . Delicious Bookmark this on Delicious