6

I'm looking for the best way to initialize an array by specifying its size and a default value (here the default value will be "").

For example if i've got :

def myTab = ["","","","","","","","",""]

How can i initialize this same array without writing each field, only by changing the size and/or default value ?

Something like

def myTab = new String[9] //(combined with a 'withDefault' method equivalent)

1 Answer 1

8

You could do:

def myTab = [""] * 9

Btw, that's a list, not an array

If you really need an array (which you probably don't), you can do

String[] myTab = [""] * 9
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, the list will definitely be fine

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.