3
初始化矩陣的最簡單方法是什麼?在groovy中初始化矩陣
// something like this would be nice
int[][] matrix = new int[][]{{1,2,3},{4,5,6},{7,8,9}};
初始化矩陣的最簡單方法是什麼?在groovy中初始化矩陣
// something like this would be nice
int[][] matrix = new int[][]{{1,2,3},{4,5,6},{7,8,9}};
如果要定義變量類型,使用此:
int[][] matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
如果變量是無類型,使用此:
def matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] as int[][]
在groovysh我做
groovy:000> int[][] matrix = [[1,2,3],[4,5,6],[7,8,9]]; println matrix[1][1]; println matrix.class
5
class [[I
===> null
請注意,有一個警告「小心:我們不支持原生多維數組創建現在。」這裏找到:http://groovy.codehaus.org/Migration+From+Classic+to+JSR+syntax
還,我把
assert matrix instanceof int[][]
上月底,它似乎退房。
這將創建一個List <列表>,對不對?我猜這有點相當於int [] [],儘管不太完美。 –
ripper234
2011-02-14 16:15:46
@ ripper234 - 你是對的 - 編輯我的回答 – hvgotcodes 2011-02-14 16:23:14