[파일명:  testStringFindInList.groovy]------------------------------------------------
def 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;
}

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

ArrayList<String> words = ["하나", "둘", "셋", "넷", "다섯", "여섯"] as ArrayList<String>
int where

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

println("Sorting...")
Collections.sort(words)

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


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


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

Posted by Scripter
,