Add in Groovy

Find this useful? Support us: Star on GitHub 6
Category: Array | Language: Groovy

Sure! Adding an element to an array in Groovy can be done in a few different ways. Here are some examples:

1. Using the + operator:

def myArray = [1, 2, 3]
myArray = myArray + 4
println myArray // Prints [1, 2, 3, 4]

2. Using the << operator:

def myArray = [1, 2, 3]
myArray << 4
println myArray // Prints [1, 2, 3, 4]

3. Using the add() method:

def myArray = [1, 2, 3]
myArray.add(4)
println myArray // Prints [1, 2, 3, 4]

I hope that helps! Let me know if you have any questions.