Initiate matrix in groovy

What is an easy way to initialize a matrix?

// something like this would be nice
int[][] matrix = new int[][]{{1,2,3},{4,5,6},{7,8,9}};
+3
source share
2 answers

if you want to determine the type of a variable, use this:

int[][] matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

if the variable is untyped, use this:

def matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] as int[][]
+4
source

in groovysh i did

groovy:000> int[][]  matrix = [[1,2,3],[4,5,6],[7,8,9]]; println matrix[1][1]; println matrix.class
5
class [[I
===> null

Please note that there is a warning "Be careful: we do not support the built-in multi-dimensional array creation right now." found here: http://groovy.codehaus.org/Migration+From+Classic+to+JSR+syntax

too, I put

 assert matrix instanceof int[][]

at the end and seems to check.

+1
source

Source: https://habr.com/ru/post/1792025/


All Articles