Create populated in Groovy

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

In Groovy, we can create an array using the following syntax:

def myArray = [element1, element2, ..., elementN]

Here is an example of creating an array of integers:

def myIntArray = [1, 2, 3, 4, 5]

And here is an example of creating an array of strings:

def myStringArray = ['apple', 'banana', 'orange', 'grape', 'watermelon']

We can also create an empty array and add elements to it later:

def myEmptyArray = []
myEmptyArray.add('element1')
myEmptyArray.add('element2')

It's important to note that Groovy is dynamically typed, so arrays can also contain elements of different types. For example:

def myMixedArray = ['apple', 2, true, 4.5, 'orange']