curric

View on GitHub

./curric/oop/Lists

List

The most common Interface

Lets take a couple steps back for a second

Another type of data structure most commonly used in Computer Programming is called the List. Lists are different from Arrays, in the fact that Arrays have a set size, while a Lists’ size is mutable. Meaning that a Lists’ size can change based on the number of elements put into it. However, a List in Java is an interface, the implementation of the List is most commonly used by a type called ArrayList

To create an ArrayList, you use the new keyword, just like any other object. However, you must also include the type parameter, as ArrayLists and Lists utilize generic type parameters.

// import java.util.ArrayList; importing ArrayLists
// to cover all basis we use
import java.util.*; // the * symbol means import everything from the utils package

// To create an list
ArrayList<Integer> list = new ArrayList<Integer>();
// we must you Integer, the object wrapper for ints, as type parameters only allow of objects (aka classes)

// populate the list with every int from 0 to 4
for (int i = 0; i < 5; i++) {
    list.add(i);
}

System.out.println(list); // [0, 1, 2, 3, 4]

Essential List Functions

Reference to Oracle Docs for Lists Reference to Oracle Docs for ArrayLists