ArrayList 연습

개발/Java 2006/09/05 11:20

ArrayList라는걸 한번 써봤다
eclipse 3.2 , jdk 5.0 이어서 그런지
자동완성으로 class<Element>가 자동으로 붙었다!!

[CODE type="java"]
import java.util.ArrayList;
import java.util.ListIterator;

class Buddy{
[tab]protected String loginName = null;
[tab]protected String nickName = null;
[tab]public Buddy() {
[tab][tab]
[tab]}
[tab]public Buddy(String loginName, String nickName) {
[tab][tab]this.loginName = loginName;
[tab][tab]this.nickName = nickName;
[tab]}
[tab]public String toString(){
[tab][tab]return "LName:" + loginName + "/NName:" + nickName;
[tab]}
}

public class DStest {
[tab]
[tab]private ArrayList<Buddy> list = null;
[tab]
[tab]public DStest() {
[tab][tab]list = new ArrayList<Buddy>();
[tab][tab]
[tab]}
[tab]public void add(Buddy e)
[tab]{
[tab][tab]list.add(e);
[tab]}
[tab]public Buddy getBuddy(int idx){
[tab][tab]return list.get(idx);[tab][tab]
[tab]}
[tab]
[tab]
[tab]
[tab]public void sortList(){
[tab][tab]Buddy tmp;
[tab][tab]for(int i=0;i<list.size();i++){
[tab][tab][tab]for(int j=i;j<list.size();j++)
[tab][tab][tab][tab]if(list.get(i).nickName.compareTo(list.get(j).nickName)>0){
[tab][tab][tab][tab][tab]tmp = list.get(i);
[tab][tab][tab][tab][tab]list.set(i, list.get(j));
[tab][tab][tab][tab][tab]list.set(j, tmp);
[tab][tab][tab][tab]}
[tab][tab]}
[tab]}
[tab]
[tab]public void printList(){
[tab][tab]ListIterator<Buddy> li = list.listIterator();
[tab][tab]Buddy cur;
[tab][tab]while(li.hasNext()){
[tab][tab][tab]cur = li.next();
[tab][tab][tab]System.out.println(cur);[tab][tab][tab]
[tab][tab]}
[tab][tab]
[tab]}
[tab]
[tab]public static void main(String args[]){
[tab][tab]DStest test = new DStest();
[tab][tab]test.add(new Buddy("foobar","asdfefef"));
[tab][tab]test.add(new Buddy("thisff","dsfasdfasdf"));
[tab][tab]test.add(new Buddy("asdfaf","asdfae54ewf"));
[tab][tab]test.add(new Buddy("vzxvsd","zxvcxcerqer"));
[tab][tab]test.add(new Buddy("zxcvwe","asdfer2wera"));
[tab][tab]test.add(new Buddy("saerer","zvczxcadfwe"));
[tab][tab]test.add(new Buddy("xvas43","zxvcvawer3r"));
[tab][tab]test.add(new Buddy("cxvaer","cxvaerqwer2"));
[tab][tab]
[tab][tab]test.printList();
[tab][tab]System.out.println("========SORT=============");
[tab][tab]test.sortList();
[tab][tab]
[tab][tab]test.printList();
[tab][tab][tab]
[tab]}

}

[/HTML][/CODE]

실행결과

LName:foobar/NName:asdfefef
LName:thisff/NName:dsfasdfasdf
LName:asdfaf/NName:asdfae54ewf
LName:vzxvsd/NName:zxvcxcerqer
LName:zxcvwe/NName:asdfer2wera
LName:saerer/NName:zvczxcadfwe
LName:xvas43/NName:zxvcvawer3r
LName:cxvaer/NName:cxvaerqwer2
========SORT=============
LName:asdfaf/NName:asdfae54ewf
LName:foobar/NName:asdfefef
LName:zxcvwe/NName:asdfer2wera
LName:cxvaer/NName:cxvaerqwer2
LName:thisff/NName:dsfasdfasdf
LName:saerer/NName:zvczxcadfwe
LName:xvas43/NName:zxvcvawer3r
LName:vzxvsd/NName:zxvcxcerqer
2006/09/05 11:20 2006/09/05 11:20

Trackback Address :: 이 글에는 트랙백을 보낼 수 없습니다


: [1] : ... [7] : [8] : [9] : [10] : [11] : [12] : [13] : [14] : [15] : ... [28] :