[파일명:  TestStringFindInList.java]------------------------------------------------
import java.util.*;

public class TestStringFindInList {

    public static void main(String[] args) {
        String[] data = { "하나", "둘", "셋", "넷", "다섯", "여섯" };
        ArrayList<String> words = new ArrayList<String>();
        for (int i = 0; i < data.length; i++) {
            words.add(data[i]);
        }
        int where;

        System.out.print("vector: ");
        printArray(words);
        where = find(words, "셋");
        if (where > 0) {
            System.out.print("발견!  ");
            System.out.println("Next word of 셋 in list: " + words.get(where+1));
        }

        System.out.println("Sorting...");
        Collections.sort(words);

        System.out.print("vector: ");
        printArray(words);
        where = find(words, "셋");
        if (where > 0) {
            System.out.print("발견!  ");
            System.out.println("Next word of 셋 in list: " + words.get(where+1));
        }
    }

    static int find(ArrayList<String> arr, String s) {
     for (int i = 0; i < arr.size(); i++) {
      if (arr.get(i).indexOf(s) >= 0)
       return i;
     }
     return -1;
    }

    static void printArray(ArrayList<String> arr) {
        System.out.print("[");
        for (int i = 0; i < arr.size() - 1; i++) {
            System.out.print(arr.get(i) + ", ");
        }
        if ( arr.size() > 0)
            System.out.print(arr.get(arr.size() - 1));
        System.out.println("]");
    }
}
------------------------------------------------


컴파일> javac testStringFindInList.java

실행> java TestStringFindInList
list: [하나, 둘, 셋, 넷, 다섯, 여섯]
발견!  Next word of 셋 in list: 넷
Sorting...
list: [넷, 다섯, 둘, 셋, 여섯, 하나]
발견!  Next word of 셋 in list: 여섯


크리에이티브 커먼즈 라이선스
Creative Commons License

Posted by Scripter
,