1

I need to call a Java method from Xtend that expects a two dimensional array such as:

javafunc (String [][] {{"0","1"}.{"2","3"}})

My initial thought is to write a Java static method that accepts a List of arrays, converts it to a two dimensional array and calls the underlying Java method. Before I do this I would like to know if there are better solutions. This one is a bit ugly.

Jonathan
  • 2,635
  • 3
  • 30
  • 49

1 Answers1

0

If you want to avoid using arrays, you could use a list of pairs.

#["0" -> "1", "1" -> "2"]

I would convert it using a extension function instead of writing a wrapper for the java function. That way it is more resuable.

javafunc(#["0" -> "1", "1" -> "2"].toArrays)

def String[][] toArrays(Iterable<Pair<String,String>> pairs) {
    // convert pairs to 2d array ...
}

Whether this is a good solution depends a bit on what the data represents though, pairs can be confusing if there is no obvious relationship between key and value.

kapex
  • 28,903
  • 6
  • 107
  • 121