Welcome aboard our Java Voyage! Today, we're learning about ArrayLists
. They're similar to arrays but can resize dynamically and also have many additional ways to simplify our lives. By the end, you'll know how to create and manipulate an ArrayList
and understand when it's preferable to use arrays.
ArrayLists
are akin to a flexible roster of interstellar explorers; their size changes as we encounter or lose crew members. ArrayLists
, part of Java's Collections Framework
, elevate arrays to another level by providing more flexibility.
Constructing an ArrayList
is akin to assembling a crew list. As shown, we declare a variable of type ArrayList
:
Here:
List<Integer>
is a general type for all lists of integerscrewMembers
is a list namenew ArrayList<>()
creates an instance of the list of integers. Note the part denoting that can be of any type ( etc.), but we already specified the type when mentioning it'd be at the beginning of the definition.
