我想将数组存储在数组中,但我不知 Prop 体该怎么做。
我想要的是: 我有一个名为 array 的数组。
在我想要将一个项目附加到该数组的方法中,该项目也将是一个数组。
例如,这将位于我的第一个数组中:(调用方法时会附加其中的每个项目)
{1,2},{2,3},{5,6}
谢谢。
最佳答案
要纯粹使用数组,请参阅:http://www.ensta.fr/~diam/java/online/notes-java/data/arrays/arrays-2D-2.html
例如,分配您可能执行的所有操作:
int[][] tri;
//... Allocate each part of the two-dimensional array individually.
tri = new int[10][]; // Allocate array of rows
for (int r=0; r < 2; r++) {
tri[r] = new int[2]; // Allocate a row
}
但是,如果您需要支持追加操作,您最好使用其他数据结构(例如 List、ArrayList 等)来保存顶级“数组”。这样你就可以将数组附加到它,而不必玩游戏重新分配它。 Sean 的解决方案非常适合这个问题。
关于java - 如何处理多维数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2328795/