Definition
Lists are data structures used when the number of elements is unknown.
- Lists are without index.
- Traditionally, only the first (head) and the remaining items (tail) can be viewed
- Lists have dynamic size, and can grow or shrink as needed.
- Items are added (to the end) via appending
Signature
name: list
import: element, boolean
ops:
newList : → list;
first : list → element;
rest : list → list;
insert : list × element → list;
contains : list × element → boolean;
append : list × element → list;
isEmpty : list → boolean;
etc. (more operations can be defined)
Implementation
In Java
Java uses the ArrayList Class to create lists. Multiple data types can be stored in such lists by using the Object
class, which all classes in Java inherit from.
import java.util.ArrayList;
ArrayList<String> stringList = new ArrayList<>();
stringList.add(new String("hi"));
ArrayList<Object> genericList = new ArrayList<>();