I want to implement a data structure which looks something like this.
{{RowID, N1, N2, N3},
{RowID, N4, N5, N6},
{RowID, N7, N8, N9}}
And goes on. It basically is a table in Java with 3 columns and RowID. What data structure should I use and how do I implement it as in code?
Make an ArrayList of ArrayLists. E.g.:
ArrayList<ArrayList> arrayListOfLists = new ArrayList<ArrayList>();
arrayListOfLists.add(anotherArrayList);
//etc...
There are several options. One way is to declare a class that represents a row.
public class MyRow{
private long rowId;
private int col1;
private int col2;
private int col3;
//etc
}
Obviously you choose appropriate data types and variable names.
Then you can create an ArrayList of this type:
List<MyRow> rows = new ArrayList<MyRow>();
This is especially useful if the number of columns will not vary.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With