Intro

  • A reference type
  • Array cannot be modified while ArrayLists can change their size
  • Can add, find size, clear, remove a index, get an index, or check if a list is empty

Traversing ArrayLists

  • Using for loop
  • Using while loop
  • Enhanced for loop
    • example
for(String name:roster){}

Developing Algorithms with Array Lists

  • arraylist can change size so a lot more versatile than an array

Searching

  • data: arrays, lists, queue, stack, etc
  • Loop with an if statement inside, going through each index and checking if a certain condition is met
  • Order matters occasionally

Sorting

  • Sorting can work in either ascending or descending order

Homework!

import java.util.ArrayList;
import java.util.Collections;

ArrayList<String> countries = new ArrayList<>();
countries.add("USA");
countries.add("China");
countries.add("Russia");
countries.add("France");

public void print(ArrayList<String> countries){
    for (String str : countries){
        System.out.println(str);
    }
}

Collections.sort(countries, Collections.reverseOrder());
System.out.println("Reverse: ");
print(countries);
System.out.println("----------");

String tmp = countries.get(0);
countries.set(0, countries.get(countries.size()-1));
countries.set(countries.size()-1, tmp);
print(countries);
Reverse: 
USA
Russia
France
China
----------
China
Russia
France
USA