java 在JAVA中存储二维数组的数据结构
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10785760/
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
Data structure to store 2D-arrays in JAVA
提问by Christos Baziotis
i am looking for a data structure to store two dimensional integer arrays. Is List the rigth data structure or should i use another one?
我正在寻找一种数据结构来存储二维整数数组。List 是正确的数据结构还是我应该使用另一个?
Can someone give me a short example on how to create such a data structure and how to add a 2d array?
有人能给我一个关于如何创建这样的数据结构以及如何添加二维数组的简短示例吗?
Edit: I want a data structure in which i want to store int[11][7] arrays. For instance ten, int[11][7] arrays.
编辑:我想要一个数据结构,我想在其中存储 int[11][7] 数组。例如十个, int[11][7] 数组。
采纳答案by wattostudios
If you need to store a number of int[][]
arrays in a data structure, I would probably recommend that you store the int[][]
arrays in an Object
that represents what the data contains, then store these Objects
in an ArrayList
.
如果您需要int[][]
在数据结构中存储多个数组,我可能会建议您将int[][]
数组存储在Object
表示数据包含的内容Objects
的ArrayList
.
For example, here is a simple Object
wrapper for your int[][]
arrays
例如,这是一个简单Object
的int[][]
数组包装器
public class 2DArray {
int[][] array;
public 2DArray(int[][] initialArray){
array = initialArray;
}
}
And here is how you would use them, and store them in an ArrayList
这是您将如何使用它们,并将它们存储在 ArrayList
// create the list
ArrayList<2DArray> myList = new ArrayList<2DArray>();
// add the 2D arrays to the list
myList.add(new 2DArray(myArray1));
myList.add(new 2DArray(myArray2));
myList.add(new 2DArray(myArray3));
The reason for my suggestion is that your int[][]
array must have some meaning to you. By storing this in an Object
wrapper class, you can give it a meaning. For example, if the values were co-ordinates, you would call your class Coordinates
instead of 2DArray
. You, therefore, create a List
of Coordinates
, which has a lot more meaning than int[][][]
.
我提出这个建议的原因是你的int[][]
数组对你来说一定是有一定的意义的。通过将其存储在Object
包装类中,您可以赋予它一个含义。例如,如果值是坐标,您将调用您的类Coordinates
而不是2DArray
. 因此,您创建了一个List
of Coordinates
,它比 具有更多的意义int[][][]
。
回答by Edwin Buck
An array is not just an idea about how to store information, it is also an implementation of how to store data. Thus, if you use an array, you have already selected your data structure.
数组不仅仅是一个关于如何存储信息的想法,它也是如何存储数据的一个实现。因此,如果您使用数组,则您已经选择了您的数据结构。
If you want to store data in a data structure, you need to concentrate on how the data structure is used, think about how you will retrieve data and store data, how often you do each operation, and how much data you will be working with. Then you know which methods must be optimum, and have an idea of whether the data can reside in memory, etc.
如果要将数据存储在数据结构中,则需要专注于如何使用数据结构,考虑如何检索数据和存储数据,每次操作的频率以及将使用多少数据. 然后你知道哪些方法必须是最优的,并且知道数据是否可以驻留在内存中等等。
Just to give you an example of how many ways this could be solved:
只是举个例子说明有多少方法可以解决这个问题:
- You could flatten the array into a 1D array, and use x*num_columns+y as the index
- You could create an Object to contain the pair, and put the array in a Map
- You could use a linked list containing linked lists.
- You could use a tree containing trees.
- You could use a list containing trees.
- You could create a partial order over the pair and then put all the elements into one tree.
- 您可以将数组展平为一维数组,并使用 x*num_columns+y 作为索引
- 您可以创建一个对象来包含该对,并将数组放入 Map
- 您可以使用包含链表的链表。
- 您可以使用包含树木的树。
- 您可以使用包含树木的列表。
- 您可以在该对上创建偏序,然后将所有元素放入一棵树中。
All of these solutions depend heavily on which operations are more important to optimize. Sometime it is more important to update the data structure quickly, sometimes not. The deciding factor is really the rest of your program.
所有这些解决方案都在很大程度上取决于哪些操作需要优化。有时快速更新数据结构更重要,有时则不然。决定因素实际上是程序的其余部分。
回答by Mark Elliot
So you want to store a collection of 2D arrays: if the collection is fixed size add another dimension:
所以你想存储一个二维数组的集合:如果集合是固定大小,添加另一个维度:
int[][][] arrColl
If the collection is variably sized, use your favorite implementation of Collection<int[][]>
(ArrayList, LinkedList, etc.):
如果集合的大小可变,请使用您最喜欢的Collection<int[][]>
(ArrayList、LinkedList 等)实现:
Collection<int[][]> arrColl
回答by Bhaskar
based on your edits :
根据您的编辑:
List<Integer[][]>
is what you need - this will allow you to add any numbers of 2D Integer
arrays. Note that this will involve boxing and unboxing - something that should be avoided if possible.
List<Integer[][]>
是您所需要的 - 这将允许您添加任意数量的二维Integer
数组。请注意,这将涉及装箱和拆箱 - 如果可能,应该避免这种情况。
If it suffices ( if you know how many 2D int arrays you need in advance ), you can even use int[][][]
- a 3D array of ints - this does not involve boxing/unboxing.
如果足够(如果您事先知道需要多少个 2D int 数组),您甚至可以使用int[][][]
- 一个 3D 整数数组 - 这不涉及装箱/拆箱。
回答by dbf
If size is fixed, then use int[][]
else List<List<Integer>>
.
如果大小是固定的,则使用int[][]
else List<List<Integer>>
。