Skip to content

Creating ArrayLists

  • Lesson
  • 1 min read
  • 2 of 3 in this chapter

Array what?

In the last chapter, we had talked about Arrays. And while they are all great, they have a few issues:

  • We cannot add more values to the existing array
  • We cannot remove values from the existing array
  • It isn't easy to move values in the array around

The solution to these problems? ArrayList.

Creating ArrayLists

First, we need to import the ArrayList class.

Java
import java.util.ArrayList;

When we create an ArrayList of Strings, we essentially write: "Create an ArrayList of Strings that is a new ArrayList of Strings."

And we can write this like so:

Java
ArrayList<String> names = new ArrayList<String>();

We just created a new ArrayList object named names. Notice how we haven't added any values yet. We do that after we create the object itself.

In the next lesson, we will learn about all the methods we can use with ArrayList.