Lists

Data Types and Exceptions in Java

Jim White

Java Developer

List interface

  • List interface, sub-interface of Collection
    • Defines an ordered group of objects
    • Can contain duplicated objects
  • Several implementations of List
    • All are similar in behavior
    • Implementation determines how objects are managed under the covers
Data Types and Exceptions in Java

List implementations

  • ArrayList - a resizable, indexed (like arrays), ordered list of objects

ArrayList are similar to Java arrays with indexed elements

  • LinkedList - objects connected by links to the next and previous objects.

LinkedList are object lists connected by pointers

  • No matter the implementation, add, remove, change objects in the same way
1 See https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/util/List.html for List types and details
Data Types and Exceptions in Java

ArrayList

  • Construct an instance with new and use generics to specify content type
  • Add an object to the list end with .add(object)
  • Access an object with .get(index)
    • ArrayList has zero-based index
  • Replace/change an object with .set(index, object)
  • Remove an object with .remove(index)
  • Clear or empty all objects with .clear()
  • Count of objects with .size()
import java.util.ArrayList;
...
ArrayList<String> animals
  = new ArrayList<String>();

animals.add("horse"); animals.add("cow"); animals.add("horse"); // Duplicates allowed
String c = animals.get(1); // c="cow" animals.set(1, "chicken");
// Removes the first horse animals.remove(0); // Removes all objects animals.clear();
animals.size();
Data Types and Exceptions in Java

Objects and primitives

  • Use wrappers to add a primitive to any Collections Framework object
  • Primitives added are "autoboxed"
    • Autobox: process of automatically putting primitive in a wrapper
ArrayList<Integer> list = new ArrayList<Integer>();
list.add(new Integer(5));
list.add(6);  // 6 is automatically wrapped by Integer then added
  • Autoboxing works with all Collections Framework objects
Data Types and Exceptions in Java

Heterogeneous collections

  • Collections Framework data structures can be heterogenous
    • Heterogenous = hold objects of different types
    • Use Object as the parameterized type
ArrayList<Object> list = new ArrayList<Object>(); // Allow any object
list.add(new Integer(5)); // Add an Integer
list.add(6); // Add an Integer using autoboxing
list.add("Hello"); // Add a String
Data Types and Exceptions in Java

Looping a List

  • Use the "for each" syntax to loop through the objects of a List
ArrayList<String> animals = new ArrayList<String>();
animals.add("horse");
animals.add("cow");
animals.add("chicken");

for (String animal : animals) { // fore-each to loop through all in the ArrayList
    System.out.println(animal);
}
horse
cow
chicken
Data Types and Exceptions in Java

List println

  • The contents of a List instance (like ArrayList) can be displayed with println
ArrayList<String> animals = new ArrayList<String>();
animals.add("horse");
animals.add("cow");
animals.add("chicken");
System.out.println(animals);  // Display all the elements in the ArrayList

ArrayList<Object> list = new ArrayList<Object>();
list.add(5);
list.add("Hello");
System.out.println(list);  // Display all the elements in the ArrayList
[horse, cow, chicken]
[5, Hello]
Data Types and Exceptions in Java

LinkedList

  • LinkedList are constructed like ArrayList instances
  • LinkedList have the same methods as ArrayList
import java.util.LinkedList;
...
LinkedList<String> cars // Create a new ...
  = new LinkedList<String>(); // ... LinkedList
cars.add("Ford"); // Add an object to the list
cars.add("Mercedes");
String c = cars.get(1);
cars.set(1, "Toyota"); // Replace an object
System.out.println(cars); // Display the list
cars.remove(0); // Remove an object
cars.clear();  // Remove all objects
cars.size(); // Get the list length
[Ford, Toyota]
Data Types and Exceptions in Java

Additional LinkedList methods

  • Some additional methods
    • addFirst() add to start of the list
    • addLast() add to the end of the list
    • removeFirst() remove from start
    • removeLast() remove from end
cars.addFirst("Fiat"); // Add to the beginning
cars.addLast("BMW"); // Add the the end
cars.removeFirst(); // Remove the first object
cars.removeLast(); // Remove the last object
Data Types and Exceptions in Java

Collection type similarities

  • ArrayList and LinkedList look the same
    • Other types in Collections Framework will also have similarities
    • This is on purpose
  • They share an interface: List
    • An example of polymorphism ("many-forms")
Data Types and Exceptions in Java

ArrayList vs LinkedList

  • Operations on ArrayList and LinkedList (or any List) are the same
    • Pick of List type depends on use
  • ArrayList considerations:
    • Fast at random access (ex: list.get(11))
    • Slower at adding and removing elements in the middle
    • Because objects must physically moved when inserting/removing objects

Adding objects in the middle of an ArrayList requires lost of moving

Data Types and Exceptions in Java

ArrayList vs LinkedList

  • LinkedList considerations:
    • Slower at random access (ex: list.get(11))
    • Faster at adding and removing elements in the middle.
    • Because no objects are physically moved, just a couple of references changed

Adding objects in the middle of a LinkedList only requires changing some pointers

Data Types and Exceptions in Java

Let's practice!

Data Types and Exceptions in Java

Preparing Video For Download...