本文介绍了在 Java 中创建动态 2D 矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想要一个动态矩阵,未知的行数和列数,通过单击按钮来填充它.但是还有更多:我不想添加整行,但一次只添加一个单元格,单击= 添加一个单元格.当然不是随机的:第一行的第一个单元格,第一行的第二个单元格......然后第二行的相同......
I want a dynamic matrix, number rows and columns unkonw, filling it by clicking on a button. Bu there is more: I don't want to add entire rows, but just one cell at the time, one click = one cell added. Of course not randomly : 1st cell of 1st row, 2nd cell of 1st row... and then the same of the 2nd row and so one...
我知道 UJMP、ArrayList,但这不是我想要的.请准确回答,提前谢谢.
I know about UJMP, ArrayList, but it's not quite what I'm looking for. Please be accurate on your answer, thank you in advance.
推荐答案
使用这个:
List<List<Integer>> dynamicMatrix = new ArrayList<List<Integer>>();
dynamicMatrix.add(new ArrayList<Integer>());
dynamicMatrix.add(new ArrayList<Integer>());
dynamicMatrix.add(new ArrayList<Integer>());
dynamicMatrix.get(0).add(6);
dynamicMatrix.get(0).add(7);
dynamicMatrix.get(0).add(8);
System.out.println(dynamicMatrix.get(0).get(0)); // 6
System.out.println(dynamicMatrix.get(0).get(1)); // 7
System.out.println(dynamicMatrix.get(0).get(2)); // 8
这篇关于在 Java 中创建动态 2D 矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!