How to implement a nested ArrayList?

I want to implement a data structure that looks something like this.

{{RowID, N1, N2, N3},
 {RowID, N4, N5, N6},
 {RowID, N7, N8, N9}}

And go on. This is mainly a Java table with 3 columns and a RowID. What data structure should be used and how to implement it, as in the code?

+3
source share
6 answers

Assuming the RowID is long and the column data is Doubles, I would implement this construct as:

import java.util.HashMap;
import java.util.Map;
...
Map<Long, Double[]> table = new HashMap<Long, Double[]>();

To save a string:

Long rowID = 1234L;
table.put(rowID, new Double {0.1, 0.2, 0.3});

To access the line:

Double[] row = table.get(rowID);

Replace Double [] with any desired data type Int [], String [], Object [] ...

You can skip this data with an iterator:

import java.util.Iterator;
import java.util.Map.Entry;
...
Iterator<Entry<Long, Double[]>> iter = table.entrySet().iterator();
while (iter.hasNext()) {
    Entry entry = iter.next();
    rowID = entry.getKey();
    row = entry.getValue();
};

, LinkHashMap HashMap.

+2

ArrayList ArrayLists. :.

ArrayList<ArrayList> arrayListOfLists = new ArrayList<ArrayList>();
arrayListOfLists.add(anotherArrayList);
//etc...
+10

. - , .

 public class MyRow{
     private long rowId;
     private int col1;
     private int col2;
     private int col3;
     //etc
 }

, .

ArrayList :

   List<MyRow> rows = new ArrayList<MyRow>();

, .

+6

Map<Integer, ArrayList<MyObject>>, RowID.

+2

bean, . " ArrayList", .

beans , , LinkedList, . , ArrayList.

If the order is not important, you can use HashSet or HashMap instead, depending on whether you only iterate through it (Set) or if you need to search for keys using the RowID (Map). If you are using one of these data structures, you need to redefine equals()and hashCode()to bean.

+1
source

Java provides a list listing, so you can do it like this:

ArrayList<List<someObject>> ArrayListOfLists = new ArrayList<List<someObject>>();
0
source

Source: https://habr.com/ru/post/1773662/


All Articles