java Java动态二维矩阵
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5533484/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java dynamic 2D matrix
提问by guts
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.
l 想创建一个动态二维矩阵,其中行数和列数未知。通过一次添加一个元素来填充它。例如,第一个按钮点击= M[1][1](此时矩阵只包含这个元素),然后M[1][2]、[1][3]....等。
回答by lukastymo
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
回答by WhiteFang34
Here's one option to consider to keep your 2D array fast for processing. It starts with a fixed size array of int[][]
and grows only as necessary:
这是一个可以考虑保持二维数组快速处理的选项。它从一个固定大小的数组开始,int[][]
并仅在必要时增长:
public class DynamicMatrix2D {
private int[][] matrix = new int[5][5];
public void set(int x, int y, int value) {
if (x >= matrix.length) {
int[][] tmp = matrix;
matrix = new int[x + 1][];
System.arraycopy(tmp, 0, matrix, 0, tmp.length);
for (int i = x; i < x + 1; i++) {
matrix[i] = new int[y];
}
}
if (y >= matrix[x].length) {
int[] tmp = matrix[x];
matrix[x] = new int[y + 1];
System.arraycopy(tmp, 0, matrix[x], 0, tmp.length);
}
matrix[x][y] = value;
}
public int get(int x, int y) {
return x >= matrix.length || y >= matrix[x].length ? 0 : matrix[x][y];
}
public static void main(String[] args) {
DynamicMatrix2D matrix2d = new DynamicMatrix2D();
matrix2d.set(1, 1, 1); // set (1, 1) to 1
matrix2d.set(10, 10, 2); // set (10, 10) to 2
matrix2d.set(100, 100, 3); // set (100, 100) to 3
System.out.println(matrix2d.get(1, 1)); // outputs 1
System.out.println(matrix2d.get(10, 10)); // outputs 2
System.out.println(matrix2d.get(100, 100)); // outputs 3
}
}
回答by void-pointer
You could (1) use a hash map which maps points to button states and have the maximum number of rows and columns stored in separate variables; alternatively, you could (2) use a tree and have one node for each row, and add nodes to the corresponding row nodes to represent matrix entries.
您可以 (1) 使用哈希映射将点映射到按钮状态,并将最大行数和列数存储在单独的变量中;或者,您可以 (2) 使用一棵树,每行有一个节点,并将节点添加到相应的行节点以表示矩阵条目。
You could also (3) use an ordered, dynamic list (array list, linked list, etc.) of integers, where the first nbits of each integer can store the row, the next nbits the column, and the rest of the bits any data pertaining to the state of the button. The size of n, however, depends on what your maximum bounds for the number of rows and columns are. Use bitwise operators to extract relevant data when you retrieve an element from the list.
您还可以 (3) 使用整数的有序动态列表(数组列表、链表等),其中每个整数的前n位可以存储行,接下来的n位可以存储列,其余的可以存储位与按钮状态有关的任何数据。但是,n的大小取决于行数和列数的最大界限。从列表中检索元素时,使用按位运算符提取相关数据。
The amount of memory allocated would be least for (3) if an array list is used, but otherwise, each entry will have some extra data associated with it when you add extra elements, due to the nature of the data structure. Searching would be fastest with (1); both (2) and (3) should exhibit O(log(n)) searching times, but I would suspect that (3) would be significantly faster because of the data locality. Of approaches (1) and (2), adding and removing elements would be fastest with (1); the time it takes for approach (3) to add or remove an element depends on the implementation of the list.
如果使用数组列表,则为 (3) 分配的内存量最少,但否则,由于数据结构的性质,当您添加额外元素时,每个条目都会有一些与其关联的额外数据。使用 (1) 搜索会最快;(2) 和 (3) 都应该展示 O(log(n)) 搜索时间,但我怀疑由于数据局部性,(3)会明显更快。在方法(1)和(2)中,使用(1)添加和删除元素最快;方法 (3) 添加或删除元素所需的时间取决于列表的实现。
I'm sure there's plenty of other structures which you could use that I haven't listed here, but you may want to note that if you can guarantee that the number of rows and columns will remain within reasonable bounds, then using a static data structure could really speed things up.
我确定还有很多其他结构可供您使用,但我没有在此处列出,但您可能需要注意,如果您能保证行数和列数保持在合理范围内,则使用静态数据结构真的可以加快速度。