Java的动态二维矩阵矩阵、动态、Java

由网友(且以深情共白头)分享简介:升希望创建一个动态的2D矩阵,其中行和列的数目是未知的。通过添加一个元件在时刻填充它。例如,第一按钮点击= M [1] [1](在这个时候,在矩阵只包含该元素),则M [1] [2],[1] [3] ....等。l would like to create a dynamic 2D matrix, where the...

升希望创建一个动态的2D矩阵,其中行和列的数目是未知的。通过添加一个元件在时刻填充它。例如,第一按钮点击= M [1] [1](在这个时候,在矩阵只包含该元素),则M [1] [2],[1] [3] ....等。

l would like to create a dynamic 2D matrix, where the number of rows and columns is unknown. Filling it by adding one element at the time. For example, 1st button click = M[1][1] (at this time, the matrix contains only this element), then M[1][2], [1][3]....etc.

推荐答案

使用集合来做到这一点。例如:

Use collections to do this. For example:

List<List<Integer>> dynamic2D = new ArrayList<List<Integer>>();

dynamic2D.add(new ArrayList<Integer>());
dynamic2D.add(new ArrayList<Integer>());
dynamic2D.add(new ArrayList<Integer>());

dynamic2D.get(0).add(5);
dynamic2D.get(0).add(6);
dynamic2D.get(0).add(7);

System.out.println(dynamic2D.get(0).get(0)); // 5
System.out.println(dynamic2D.get(0).get(1)); // 6
System.out.println(dynamic2D.get(0).get(2)); // 7
阅读全文

相关推荐

最新文章