1

I have a arrayList done this way:

ArrayList<ArrayList<Double>> level = new ArrayList<ArrayList<Double>>(levels + 1);

So something like: enter image description here

What I would do is add a new element to the internal list saved in a certain index i of the external list. How can I do this?

I can't use method arrayList.add(i, 0.0);.

2 Answers 2

2

Use index from the external ArrayList and use it to add the number. Like this

level.get(i).add(1.1);
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks. Why I get an out of bound exception if I try to add something in index 1?
@lon What is i and what is the size of level?
i=1 and the size of level is 4
I solved using listOnList.add(new ArrayList<Double>());, thanks!
@lon was just about to write you need to initialize the inner ArrayList<Double>, not only level. Good luck :)
0

You need to get the list at a given position first and then insert into it the value desired.

Example:

final List<List<Double>> listOnList = new ArrayList<List<Double>>();
listOnList.add(new ArrayList<Double>());
listOnList.get(0).add(155.0);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.