Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I implement nested ArrayList?

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?

like image 951
js0823 Avatar asked Nov 08 '10 17:11

js0823


2 Answers

Make an ArrayList of ArrayLists. E.g.:

ArrayList<ArrayList> arrayListOfLists = new ArrayList<ArrayList>();
arrayListOfLists.add(anotherArrayList);
//etc...
like image 125
Rafe Kettler Avatar answered Oct 30 '22 20:10

Rafe Kettler


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.

like image 22
Vincent Ramdhanie Avatar answered Oct 30 '22 18:10

Vincent Ramdhanie