Monday, December 20, 2010

List In Groovy

Groovy has built in datastructure called list. It used to store ordered data. To initialize it, use:
groovy>> name = ["lady gaga", "luna maya", "barack obama"]

Use [ ] operator to access an element of the data:
groovy>> println name[0]
groovy>> println name[2]

To get the size, use:
groovy>> println name.size()

You can add an element to it by:
groovy>> data[5] = "bill clinton"

You cannot do this, because there's not index number
groovy>> data[] = "tony blair"

To create an empty list, type:
groovy>> data = []

No comments:

Post a Comment