사용자 도구

사이트 도구


groovy:list

차이

문서의 선택한 두 판 사이의 차이를 보여줍니다.

차이 보기로 링크

양쪽 이전 판 이전 판
다음 판
이전 판
groovy:list [2012/11/28 17:43]
kwon37xi
groovy:list [2018/06/21 15:48] (현재)
kwon37xi
줄 1: 줄 1:
-====== Groovy List ======+====== Groovy List / Collection ======
   * [[http://groovy.codehaus.org/Collections|Groovy Collections]]   * [[http://groovy.codehaus.org/Collections|Groovy Collections]]
   * [[http://groovy.codehaus.org/JN1015-Collections|Groovy List and Sets]]   * [[http://groovy.codehaus.org/JN1015-Collections|Groovy List and Sets]]
-  * http://groovy.codehaus.org/groovy-jdk/java/util/List.html+  * [[http://groovy.codehaus.org/groovy-jdk/java/util/List.html|List GDK]] 
 +  * [[http://groovy.codehaus.org/groovy-jdk/java/util/Collection.html|Collection GDK]] 
 +  * [[http://examples.javacodegeeks.com/core-java/groovy-array-example/|Groovy Array Example | Examples Java Code Geeks]]
   * 문자열도 문자의 리스트로 간주할 수 있음.   * 문자열도 문자의 리스트로 간주할 수 있음.
  
줄 8: 줄 10:
   * http://mrhaki.blogspot.kr/2012/10/groovy-goodness-create-list-with.html   * http://mrhaki.blogspot.kr/2012/10/groovy-goodness-create-list-with.html
 <code groovy> <code groovy>
-def lazy = ['abc', 42].withDefault { 'default' } // Or .withLazyDefault {} 접근전에는 null, 접근시에는 'default' +lazy = ['abc', 42].withDefault { 'default' } // Or .withLazyDefault {} 접근전에는 null, 접근시에는 'default' 
-def eager = ['abc', 42].withEagerDefault { 'default' } // 접근전부터 'default'+eager = ['abc', 42].withEagerDefault { 'default' } // 접근전부터 'default'
  
-def sample = [1,2,3].withDefault { index -> // index를 이용해 값 넣기+sample = [1,2,3].withDefault { index -> // index를 이용해 값 넣기
     index % 2     index % 2
 } }
줄 19: 줄 21:
   * http://mrhaki.blogspot.kr/2012/10/groovy-goodness-getting-first-and-last.html   * http://mrhaki.blogspot.kr/2012/10/groovy-goodness-getting-first-and-last.html
 <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" }
 ['a', 'b', 'c'].eachWithIndex{ it, i -> println "$i: $it" } // index 를 가지고 each 돌기. index는 0 부터 시작 ['a', 'b', 'c'].eachWithIndex{ it, i -> println "$i: $it" } // index 를 가지고 each 돌기. index는 0 부터 시작
 +
 +// Range는 괄호로 감싸서 each 해야 한다.
 +(0..100).each {
 +  println it
 +}
 </code> </code>
  
 ===== 총합과 평균 ===== ===== 총합과 평균 =====
 <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()
 +</code>
 +
 +===== 기타 =====
 +  * 섞기<code groovy>
 +Collections.shuffle(list, new Random())
 +</code>
 +  * 중복 값 제거<code groovy>
 +assert [3,5,5,5,2].unique() == [3,5,2]
 +</code>
 +  * unmodifiable<code groovy>
 +['Groovy', 'Gradle', 'Asciidoctor', 'Micronaut'].asUnmodifiable()
 </code> </code>
  
groovy/list.1354092199.txt.gz · 마지막으로 수정됨: 2012/11/28 17:43 저자 kwon37xi