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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 01:13:57  来源:igfitidea点击:

Which collection is better to store data from multi-dimensional array?

javacollectionsmultidimensional-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

ArrayListshould do what you need. For instance:

ArrayList应该做你需要的。例如:

List<List<String>> stringList = new ArrayList<List<String>>();  //A List to store a list of strings

or...

或者...

List<String[]> myNumberList = new ArrayList<List<String[]>();   //A List to store arrays of Strings.

回答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 HashMapand 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 ArrayListand LinkedList. With a LinkedListyou can insertand removean element from any position in O(1) constant time. With an ArrayListthis would be linear time O(n).

立即想到的两种数据结构是ArrayListLinkedList。有了LinkedList你可以insertremove来自任何位置的元素O(1) constant time。用ArrayList,这将是线性时间为O(n)。

However, accessing an element in an ArrayListis constant time (you can index into it). Whereas, normally with a LinkedListyou would need to traverse through it. This problem in LinkedListthough can be avoided though by hashingeach of the elements, so you can find a particular node in a linked listin amortized constant time. Of course, having a hashand a linked listis a speedier solution than an arraythough there is more overhead in terms of space.

但是,访问一个元素的ArrayList时间是恒定的(您可以对其进行索引)。而通常情况下,LinkedList您需要遍历它。LinkedList尽管hashing每个元素都可以避免这个问题 in ,但您可以在 a linked listin 中找到特定节点amortized constant time。当然,尽管在空间方面有更多的开销,但使用 ahash和 alinked list是比 an 更快的解决方案array

For more information on these data-structures: Arrays, Linked Lists, Hash Tables
Javaimplementations of these data structures: ArrayList, LinkedList, Hash table

有关这些数据结构的更多信息:ArraysLinked ListsHash Tables
Java这些数据结构的实现:ArrayListLinkedListHash table