What collection is better to store data from a multidimensional array?

I have one multi-dimensional array of string. I am ready to convert it to some type of collection so that I can add, delete and insert elements as I wish. In an array, I cannot delete an item in a specific place.

I need a collection in which I can delete data in a specific place, as well as add data to any position.
Also, don't forget that I have a multidimensional array, so the collection should also store multidimensional data.
Which collection will be suitable for my requirements?

+3
source share
3 answers

ArrayList should do what you need. For instance:

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.
+5

? ("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);
    }

HashMap, ArrayList. , . , , ,

+2

, , (, ).

, , - ArrayList LinkedList. LinkedList insert remove O(1) constant time. ArrayList O (n).

ArrayList ( ). LinkedList . LinkedList, , hashing , node linked list amortized constant time. , hash a linked list , array, .

: , , Hash Tables
Java : ArrayList, LinkedList,

0

All Articles