Advertisement
Guest User

Untitled

a guest
May 29th, 2015
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.53 KB | None | 0 0
  1. #!/usr/bin/env groovy
  2. println "Hello from the shebang line"
  3.  
  4. // ## LISTS
  5.  
  6. // default = ArrayList
  7. def arrayList = [1, 2, 3]
  8. assert arrayList instanceof java.util.ArrayList
  9.  
  10. // linkedList
  11. def linkedList = [2, 3, 4] as LinkedList
  12. assert linkedList instanceof java.util.LinkedList
  13.  
  14. LinkedList otherLinked = [3, 4, 5]
  15. assert otherLinked instanceof java.util.LinkedList
  16.  
  17. def letters = ['a', 'b', 'c', 'd']
  18.  
  19. assert letters[0] == 'a'
  20. assert letters[1] == 'b'
  21.  
  22. // access the last element of the list with a negative index: -1
  23. assert letters[-1] == 'd'
  24. assert letters[-2] == 'c'
  25.  
  26. letters[2] = 'C'
  27. assert letters[2] == 'C'
  28.  
  29. // use the << leftShift operator to append an element at the end of the list
  30. letters << 'e'
  31. assert letters[ 4] == 'e'
  32. assert letters[-1] == 'e'
  33.  
  34. // access two elements at once, returning a new list containing those two elements
  35. assert letters[1, 3] == ['b', 'd']
  36.  
  37. // Use a range to access a range of values from the list, from a start to an end element position
  38. assert letters[2..4] == ['C', 'd', 'e']
  39.  
  40. // multi-dimensional list
  41. def multi = [[0, 1], [2, 3]]
  42. assert multi[1][0] == 2
  43.  
  44. // ## ARRAYS
  45.  
  46. String[] arrStr = ['Ananas', 'Banana', 'Kiwi']
  47. def numArr = [1, 2, 3] as int[]
  48.  
  49. // ## MAPS
  50.  
  51. def colors = [red: '#FF0000', green: '#00FF00', blue: '#0000FF']
  52.  
  53. assert colors['red'] == '#FF0000'
  54. assert colors.green == '#00FF00'
  55.  
  56. colors['pink'] = '#FF00FF'
  57. colors.yellow = '#FFFF00'
  58.  
  59. assert colors.pink == '#FF00FF'
  60. assert colors['yellow'] == '#FFFF00'
  61.  
  62. assert colors instanceof java.util.LinkedHashMap
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement