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