Nicolas Martel
Nicolas Martel

Reputation: 1721

Groovy cast List<List<Integer>> into int[][]

I am embedding Groovy into my game engine and I am invoking this Groovy method from my script to get some information

def getSameTiles() {
    final int cx = 0;
    final int cy = 6;

    return  [
                [
                    [cx + 0, cy + 0],
                    [cx + 1, cy + 0],
                    [cx + 2, cy + 0],
                    [cx + 0, cy + 1],
                    [cx + 1, cy + 1],
                    [cx + 2, cy + 1],
                    [cx + 0, cy + 2],
                    [cx + 1, cy + 2],
                    [cx + 2, cy + 2],
                    [cx + 3, cy + 1],
                    [cx + 4, cy + 1],
                    [cx + 3, cy + 2],
                    [cx + 4, cy + 2],
                ],
            ];
}

I can do as List<Integer>[] but is there an elegant way to turn it into an int[][]?

Upvotes: 1

Views: 264

Answers (1)

dmahapatro
dmahapatro

Reputation: 50245

  • *.toArray() required for each entry in matrix to be an Object[].
  • Finally inferring the return as a matrix Object[][].

as shown below

def getSameTiles() {
    final int cx = 0
    final int cy = 6

    return  [
                [
                    [cx + 0, cy + 0],
                    [cx + 1, cy + 0],
                    [cx + 2, cy + 0],
                    [cx + 0, cy + 1],
                    [cx + 1, cy + 1],
                    [cx + 2, cy + 1],
                    [cx + 0, cy + 2],
                    [cx + 1, cy + 2],
                    [cx + 2, cy + 2],
                    [cx + 3, cy + 1],
                    [cx + 4, cy + 1],
                    [cx + 3, cy + 2],
                    [cx + 4, cy + 2],
                ]*.toArray()
            ] as Object[][]
}

Upvotes: 2

Related Questions