But what happens if you want an object to have the number of boxes vary as needed. Let's think of an analogy. You are having some friends over to your place, and you are somewhat cheap. You have many folding chairs that stay folded when no one is around.
Each time a guest arrives, you open a folding chair so they can sit down. Each time a guest leaves, you close the folding chair, so it doesn't take up space. You have as many chairs as guests, and no more.
For example, suppose you have a small college. There are several hundred students. Each semester, all students who have a GPA of 3.5 or higher are placed on the Dean's List, recognizing their achievements. Each semester, the number of students on the Dean's List varies. Sometimes there are lots of students, sometimes not so many.
You wish to write a program that finds all students who should be on the Dean's List, and sends them email to congratulate them.
One way to do this is to have a list of students in some container object. Then, go through the entire list, one at a time, and check the GPA. If the GPA is 3.5 or higher, add a copy the Student handle to another container, that holds the students on the Dean's List. Once you've done processing the list, go through the new container, and email each person on that list.
We'll also look at arrays. Arrays don't have the same kind of flexibility as ArrayList. Nevertheless, they are important in many languages, including Java. In general, we'll favor using ArrayList over arrays.
Then, we'll implement a simple ArrayList using arrays so you can get an idea of how to write your own ArrayList.