hesk
hesk

Reputation: 327

Java 2-D ArrayList : Adding Values.

I have to create an ArrayList of ArrayList i.e.

ArrayList<ArrayList<Integer>> l=new ArrayList<ArrayList<Integer>>();

I will be receiving the values for the inner list at random i.e. sometimes I might get the value for the first inner list, sometimes for the second. So, I have to do something like, l.get(x).add(y); But since initially all the outer list variables point at null this would give me an error. I somehow need to initialize all the outer list as an empty list [] and not null to which I can get a valid reference.

Any idea on how to get around this problem?

Upvotes: 1

Views: 54

Answers (1)

Jason K.
Jason K.

Reputation: 838

You could create your own list that extends ArrayList overriding the add() type functions and when the add() or similar function is called, ensures that the Array is at least as long as the current index + 1 before inserting the child Array.

Upvotes: 2

Related Questions