java 哪个集合更适合存储来自多维数组的数据?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10477407/
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
Which collection is better to store data from multi-dimensional array?
提问by Sachin Mhetre
I have a multi-dimensional array of string
. I am willing to convert it into some collection type, so that I can add, remove and insert elements according my wish. In array I can not remove element at particular position.
我有一个multi-dimensional array of string
. 我愿意将其转换为某种集合类型,以便我可以根据自己的意愿添加、删除和插入元素。在数组中,我无法删除特定位置的元素。
I need such collection in which I can remove data at particular position, also able to add data at any position.
Also dont forget I have multi-dimension array, so the collection should also able to store multidimensional data.
Which collection will be suitable for my requirments?
我需要这样的集合,我可以在其中删除特定位置的数据,也可以在任何位置添加数据。
另外不要忘记我有多维数组,所以集合也应该能够存储多维数据。
哪个系列适合我的要求?
回答by npinti
回答by Crazenezz
Are you sure you have Multi-Dimensionalarray? Because I look for your sample data ("yes","abbbc")
it is for One-Dimensionalarray. But let me give you an example:
你确定你有多维数组吗?因为我在寻找您的示例数据,("yes","abbbc")
所以它是用于一维数组的。但是让我给你举个例子:
// This example for multi-dimensional array of string
String[][] arrays = new String[][]{{"aa", "bb", "cc"}, {"dd", "ee", "ff"}};
Map<Integer, List<String>> map = new HashMap<>();
List<String> list;
for(int i = 0; i < arrays.length; i++) {
list = Arrays.asList(arrays[i]);
map.put(i, list);
}
for(int i = 0; i < map.size(); i++) {
for(int j = 0; j < map.get(i).size(); j++) {
System.out.println(map.get(i).get(j));
}
}
// This example for one-dimensional array of string
String[] arr = new String[] {"aa", "bb"};
List<String> listArr = Arrays.asList(arr);
for(String str : listArr) {
System.out.println(str);
}
For Multi-Dimensionalarray I'm using HashMap
and for One-Dimensionalarray I'm using ArrayList
. Read thisif you still don't understand between those two. And please Correct Me If I'm Wrong
对于我使用的多维数组HashMap
和我使用的一维数组ArrayList
。如果您仍然不明白这两者之间的区别,请阅读本文。如果我错了,请纠正我
回答by Aayush Kumar
As with any problem, you have multiple options for data-structures and you have to make a design decision based on their trade-offs (time, space).
与任何问题一样,您有多种数据结构选择,您必须根据它们的权衡(时间、空间)做出设计决策。
Two data structures that immediately come to mind are ArrayList
and LinkedList
. With a LinkedList
you can insert
and remove
an element from any position in O(1) constant time
. With an ArrayList
this would be linear time O(n).
立即想到的两种数据结构是ArrayList
和LinkedList
。有了LinkedList
你可以insert
和remove
来自任何位置的元素O(1) constant time
。用ArrayList
,这将是线性时间为O(n)。
However, accessing an element in an ArrayList
is constant time (you can index into it). Whereas, normally with a LinkedList
you would need to traverse through it. This problem in LinkedList
though can be avoided though by hashing
each of the elements, so you can find a particular node in a linked list
in amortized constant time
. Of course, having a hash
and a linked list
is a speedier solution than an array
though there is more overhead in terms of space.
但是,访问一个元素的ArrayList
时间是恒定的(您可以对其进行索引)。而通常情况下,LinkedList
您需要遍历它。LinkedList
尽管hashing
每个元素都可以避免这个问题 in ,但您可以在 a linked list
in 中找到特定节点amortized constant time
。当然,尽管在空间方面有更多的开销,但使用 ahash
和 alinked list
是比 an 更快的解决方案array
。
For more information on these data-structures: Arrays, Linked Lists, Hash TablesJava
implementations of these data structures: ArrayList, LinkedList, Hash table
有关这些数据结构的更多信息:Arrays、Linked Lists、Hash TablesJava
这些数据结构的实现:ArrayList、LinkedList、Hash table