Lists of Lists

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.
go to previous page   go to home page   go to next page        

Answer:

Should have been easy.

Lists of Lists

A list item can contain a list. For example:

  • Reds
    • red
    • pink
    • hotpink
    • lightpink
  • Oranges
    • orange
    • orangered
  • Yellows
    • yellow
    • goldenrod
    • darkgoldenrod

Examine the outer structure of the above list:

HTMLDisplay
<ul>
  <li>
  Reds
  </li>

  <li>
  Oranges
  </li>

  <li>
  Yellows
  </li>

</ul> 
  • Reds
  • Oranges
  • Yellows

(For clarity, the HTML has been indented and tags have been placed one per line. This works fine, but the browser doesn't need it done this way.) Now let us put a list into the "Red" list item. To do so, change this:

  <li>
  Reds
  </li>

to this:

  <li>
  Reds
    <ul>
    <li>red</li>
    <li>pink</li>
    <li>hotpink</li>
    <li>lightpink</li>
    </ul>
  </li>

All we have done is to make a complete list part of a List Item (which is bracketed by <li> and </li>). Here is what it looks like in context:

HTMLDisplay
<ul>
  <li>
  Reds
    <ul>
    <li>red</li>
    <li>pink</li>
    <li>hotpink</li>
    <li>lightpink</li>
    </ul>
  </li>

  <li>
  Oranges
  </li>

  <li>
  Yellows
  </li>

</ul> 
  • Reds
    • red
    • pink
    • hotpink
    • lightpink
  • Oranges
  • Yellows

QUESTION 3:

Do you suspect that it is possible to have a list of lists that contain lists?