user955732
user955732

Reputation: 1370

Push value into a List

Lets say I have list of values..

List values = [["A","A"],["B","B"],["C","C"],["D","D"]]

and I would like to push a value, "*", into the list so it looks like this

[["*", "A","A"],["*", "B","B"],["*", "C","C"],["*", "D","D"]]

Any ideas how to do this?

Thanks!

Upvotes: 0

Views: 178

Answers (3)

silverbeak
silverbeak

Reputation: 312

The star-dot method may help. It would look like this:

values*.addAll(0, "*")

The docs could use some improvement here. :)

http://docs.groovy-lang.org/latest/html/groovy-jdk/java/util/Collection.html

Upvotes: 2

tim_yates
tim_yates

Reputation: 171154

Should work:

values.collect { [ '*' ] + it }

Upvotes: 1

user955732
user955732

Reputation: 1370

For anyone interested,

this seem to work:

List values = [["A","A"],["B","B"],["C","C"],["D","D"]]

values.each{
 it.addAll(0,"*")
}

println values

OUTPUT

[[*, A, A], [*, B, B], [*, C, C], [*, D, D]]

Upvotes: 0

Related Questions