-1

So here are my declarations:

ArrayList<ArrayList<ArrayList<String>>> level1 = new ArrayList<>();
ArrayList<ArrayList<String>> level2 = new ArrayList<>();
ArrayList<String> level3 = new ArrayList<>();

So I first run through and add one level3 item to each level2 item, and then add the level2 items to level1. End result will look like this:

level1(0)
  level2(0)
   level3(0) Tall
level1(1)
   level2(0)
     level3(0) Short
level1(2)
   level2(0)
     level3(0) Thin

Now, what do I do if I want to add an additional level2? So I would take the data above and turn it into this:

level1(0)
  level2(0)
    level3(0) Tall
  level2(1)
    level3(0) Rich
level1(1)
   level2(0)
     level3(0) Short
  level2(1)
    level3(0) Nice
level1(2)
   level2(0)
     level3(0) Thin
  level2(1)
    level3(0) Mean

Doing level1.add(0, level2) doesn't work, it just pushes everything down and now level1 has 4 elements instead of 3. What am I missing?

1
  • That's to add an ArrayList to another ArrayList, I need to edit\append an ArrayList that is within an ArrayList. Commented Sep 9, 2016 at 13:12

1 Answer 1

1

The problem is you don't want to add to level1, you are trying to add to level2. First you need to get the level2 out, then you can add to it.

level1.get(0).add(level3)

Sign up to request clarification or add additional context in comments.

1 Comment

Oh, I see. I was doing level1.get(0).add(level2) and that was getting blocked.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.