Java动态二维矩阵 [英] Java dynamic 2D matrix
本文介绍了Java动态二维矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
l 想创建一个动态二维矩阵,其中行数和列数未知.通过一次添加一个元素来填充它.例如,第一个按钮点击= 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
这篇关于Java动态二维矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文