ArrayLists

What are ArrayLists?

Arraylists are a dynamic version of an array. It has special features that allow you to change the size of the arraylist. Java has a class that implements arraylists with arrays. The adding and removing function copies values from one array to another of a different size, therefore functionality is not super efficient. 

Creating an ArrayList

To use arraylists, you need to import, allowing you to access the premade Java class implementation of an arraylist.

import java.util.ArrayLists;

Declaring an ArrayList

ArrayList﹤String﹥ name ﹦ new ArrayList﹤String﹥();

You can replace the “String” with any object type (note that primitive types do not work, use Integer class for ints, Double class for doubles, etc)

Common functions of ArrayLists

.add(E obj) Adds the object that is passed in to the end of the arraylist
.add(int index, E obj) Adds the object that is passed in to the specified index.
.clear() Removes all items in the array list
.remove(int index) Removes the object at the given index
.contains(E obj) Returns true if the array contains the given value
.size() Returns the size of the arraylist
.get(int index) Returns the object at the index
.set(int index, E obj) Replaces the element at the specified position in this list with the specified element.
Full course
Next Lesson