开发者

Init a matrix in groovy

What's the easiest way to initialize a matrix?

// something like t开发者_如何学Pythonhis would be nice
int[][] matrix = new int[][]{{1,2,3},{4,5,6},{7,8,9}};


if you want to define the variable type, 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[][]


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

note that there is a warning "Be careful: we don't support native multi-dimensional array creation right now." found here: http://groovy.codehaus.org/Migration+From+Classic+to+JSR+syntax

also, I put

 assert matrix instanceof int[][]

on the end and it seems to check out.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜