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

public class TestStringFindApp {

    public static void main(String[] args) {
        String[] words = { "하나", "둘", "셋", "넷", "다섯", "여섯" };
        int where;

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

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

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

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

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


컴파일> javac testStringFindApp.java

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



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

 

Posted by Scripter
,