[파일명: 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: 여섯
'프로그래밍 > Groovy' 카테고리의 다른 글
숫자 맞추기 게임 with Groovy (0) | 2009.11.05 |
---|---|
스트링 리스트에서 스트링 찾기(find) with Groovy (0) | 2009.04.22 |
스트링 배열에서 스트링 찾기(find) with Groovy (0) | 2009.04.22 |
스트링 배열 정렬(sorting)하기 with Groovy (0) | 2009.04.15 |
Pollard's rho method 소개: 정수의 인수분해(factorizing integers) with Groovy (0) | 2009.03.24 |