user8903285
user8903285

Reputation: 35

groovy how create array with name?

IM trying to use this command in groovy but I cant print the array

appears and error

       Caught: java.lang.IllegalArgumentException: argument type mismatch

java.lang.IllegalArgumentException: argument type mismatch at Test.main(Test.groovy:7)

how I could solve this

I use this same command without this line and this works

          testArray["fff"] = "B" 

this is my code

I dont know because I cant create this 2d array

 def testArray =  []
  testArray[0] = "A"
      testArray["fff"] = "B"
      testArray[2] = "C"

   println testArray

please give me a help

Upvotes: 1

Views: 1302

Answers (2)

cfrick
cfrick

Reputation: 37043

Use testArray = [:] instead. And this is not an array but a map (a LinkedHashMap to be specific and [:] is the literal Groovy uses to create it). Maps in Java/Groovy are associative data structures to store key-value-relations. Access via map[key] is an enhancement, that Groovy brings to the table.

Upvotes: 2

Arrays only take ints as indices. "fff" is not a valid value for an index, therefore java crashes, telling you its a mismatch.

If you want to create a 2d array of Strings then you should try this.

String[][] testArray = new String[x][y];

where x and y are the dimensions for this array.

Upvotes: 1

Related Questions