Creating a dynamic 2D matrix in Java

两盒软妹~` 提交于 2019-12-04 06:24:25

问题


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...

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.


回答1:


Use this:

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


来源:https://stackoverflow.com/questions/5533214/creating-a-dynamic-2d-matrix-in-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!