문서의 선택한 두 판 사이의 차이를 보여줍니다.
양쪽 이전 판 이전 판 다음 판 | 이전 판 | ||
groovy:list [2012/11/28 17:43] kwon37xi |
groovy:list [2018/06/21 15:48] (현재) kwon37xi |
||
---|---|---|---|
줄 1: | 줄 1: | ||
- | ====== Groovy List ====== | + | ====== Groovy List / Collection |
* [[http:// | * [[http:// | ||
* [[http:// | * [[http:// | ||
- | * http:// | + | * [[http:// |
+ | * [[http:// | ||
+ | * [[http:// | ||
* 문자열도 문자의 리스트로 간주할 수 있음. | * 문자열도 문자의 리스트로 간주할 수 있음. | ||
줄 8: | 줄 10: | ||
* http:// | * http:// | ||
<code groovy> | <code groovy> | ||
- | def lazy = [' | + | lazy = [' |
- | def eager = [' | + | eager = [' |
- | def sample = [1, | + | sample = [1, |
index % 2 | index % 2 | ||
} | } | ||
줄 19: | 줄 21: | ||
* http:// | * http:// | ||
<code groovy> | <code groovy> | ||
- | def list = 0..100 | + | list = 0..100 |
assert list.first() == 0 | assert list.first() == 0 | ||
assert list.last() == 100 | assert list.last() == 100 | ||
줄 26: | 줄 28: | ||
===== sublist ===== | ===== sublist ===== | ||
<code groovy> | <code groovy> | ||
- | def list = 0..100 | + | list = 0..100 |
list[0,5] // 0~5 | list[0,5] // 0~5 | ||
list[1..-1] // 1~100까지 | list[1..-1] // 1~100까지 | ||
줄 36: | 줄 38: | ||
[1, 2, 3].each{ println "Item: $it" } | [1, 2, 3].each{ println "Item: $it" } | ||
[' | [' | ||
+ | |||
+ | // Range는 괄호로 감싸서 each 해야 한다. | ||
+ | (0..100).each { | ||
+ | println it | ||
+ | } | ||
</ | </ | ||
===== 총합과 평균 ===== | ===== 총합과 평균 ===== | ||
<code groovy> | <code groovy> | ||
- | def list = 1..100 | + | list = 1..100 |
// 5050 | // 5050 | ||
줄 47: | 줄 54: | ||
// 50.5 | // 50.5 | ||
println list.sum() / list.size() | println list.sum() / list.size() | ||
+ | </ | ||
+ | |||
+ | ===== 기타 ===== | ||
+ | * 섞기< | ||
+ | Collections.shuffle(list, | ||
+ | </ | ||
+ | * 중복 값 제거< | ||
+ | assert [3, | ||
+ | </ | ||
+ | * unmodifiable< | ||
+ | [' | ||
</ | </ | ||