1

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

6
  • 3
    Array indices should be integer values, not strings. Commented Dec 7, 2017 at 19:02
  • what is testArray["fff"] ? Commented Dec 7, 2017 at 19:05
  • 1
    hi I see that in ruby is possible create array with name but IN groovy i dont know Commented Dec 7, 2017 at 19:09
  • 1
    use testArray = [:] instead. And this is not an array but a map. I assume that this is also the case in ruby. AFAIK only PHP has some odd array/map mishmash Commented Dec 7, 2017 at 19:16
  • very thankyou cfrick works ; thanks for all Commented Dec 7, 2017 at 19:21

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

@cfrick can please help me with this problem stackoverflow.com/questions/47717505/…
1

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.

Comments

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.