Groovy web console

subscribe to the feed Subscribe
to this
site

04 Lists

Published 6 months ago by ST with tags groovy
Actions Execute script  ▶ Edit in console Back to console Show/hide line numbers View recent scripts
// create a list of items

def emptyList = []
def list = [1, 2, 3, 4, 5]
def mixed = ['I', 'am', 'ready', 2, 'go']

// retrieve items via index (0-based)

def first = list[0]
def second = list[1]
def last = list[-1]

// add item

list.add(6)
mixed << '!'

// replace an item

mixed[-2] = 'groove'

// Groovy adds a lot of useful methods
// see http://docs.groovy-lang.org/latest/html/groovy-jdk/java/util/List.html

// e.g. join elements to a string
println(mixed.join(' '))