title: To ArrayList once, or to ArrayList each time?
body: <p>I have a program which has a structure like this.</p>
  
  <pre><code>Document which contains (up to 20)
  Chapters which contain (up to 100) 
  Pages which contain (up to 20)
  Elements
  </code></pre>
  
  <p>This structure is represented by JPanels in my program. Meaning this structure has to be visually represented, and I'd rather not make a whole complex of ArrayList (unless absolutely neccessary) since each JPanels have a ZOrder of component, and a getParent() method.</p>
  
  <p>This structure is one-dimenisonal, meaning that the parent has an one-dimensional array (and when I say array, it's purely descriptive, I don't mean ArrayList or anything similar) of its children. Each individual element has an index which represents it's location in(on?) it's parent. Number of elements in a page, and pages in a chapter is inconsistent.</p>
  
  <p>It's easy to get the child's index within it's parent, but what about it's grandparents?</p>
  
  <p>Since the elements can be (and usually are) numbered, having a one numbered list per chapter, I'd have to know the index of element in the Chapter, so I can adjust the numbers when a new element is added to the list (it doesn't have to be added in the end).</p>
  
  <p>This can be solved in two ways (that I know of, that is):</p>
  
  <ol>
  <li><p>Have an ArrayList in each chapter that keeps all the elements. This would require me that, everytime I add a new element to any page, to add it to the chapter array too.
  To accomplish that I'd have to go trough all the previous pages, add up all the elements on them and add index of the new element on the present page to that number, the result being the index of the new element in the chapter, and therfore, in the array. And do that each time I add a new element.</p></li>
  <li><p>Recreate the arrayList each time I need to get the order of elements in the chapter. Which again means going trought each page and adding each element one after another until I reach the end of chapter. And I'd need it each time a new element is added.</p></li>
  </ol>
  
  <p>So the question is, which of this two methods is better (more efficient memory or processor time wise)? Which is more in the spirit of Java and programming altogether? Is there a third option that I am unaware of??</p>
  
  <p>Chapter example:</p>
  
  <pre><code>Page one {
  1. something
  2. more something
  3. nothing
  .
  .
  .
  16. still nothing
  }
  
  Page two {
  17. maybe something
  18. nope, still nothing
  .
  .
  .
  21. giberish
  }
  etc.
  </code></pre>
  
  <p>The question is: Which way of doing it is better? If you have a better idea, you can tell me, but I want to know which way of the above two is better non the less.</p>