[파일명:  testStringFindInVector.groovy]------------------------------------------------
def find(Vector<String> arr, String s) {
    for (int i = 0; i < arr.size(); i++) {
     if (arr.elementAt(i).indexOf(s) >= 0)
      return i;
    }
    return -1;
}

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


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

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

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

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


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


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

Posted by Scripter
,