Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Initialize array with size and default value

Tags:

arrays

groovy

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)
like image 592
M.Dav Avatar asked Jan 25 '17 21:01

M.Dav


1 Answers

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
like image 168
tim_yates Avatar answered Sep 18 '22 15:09

tim_yates