Tutorials
ArrayLists

Introduction

  • Like an array, an ArrayList stores a collection of similar.
  • Unlike an array, an ArrayList can only store reference types (no primitives).
  • ArrayLists use a generic between angle brackets to specify the type stored within the object. For example, creating an ArrayList of Strings would look like this:
    ArrayList<String> words = new ArrayList<>();
    
    • The left side of the assignment operator declares a reference (words) that refers to an ArrayList of Strings.
    • The right side of the assignment operator instantiates a new ArrayList of Strings that is empty (size of zero).
  • We can display all of the items in an ArrayList<String> much like we did with arrays:
    public static void display(ArrayList<String> names, PrintStream out) {
      for(int i=0; i<names.size(); ++i) {
        out.println(names.get(i));
      }
    }
    
    • We can call display as follows:
      ArrayList<String> names = new ArrayList<>();
      names.add("Peter");
      names.add("Paul");
      names.add("Mary-Anne");
      display(names, System.out);  // Note: System.out is a PrintStream object
      
    • The method will display all three names. If names had twenty names in it, all twenty would be displayed.
  • It turns out that displaying the elements in an ArrayList is actually much easier than this since the toString() method for the class has been implemented to do this work for us. Instead of calling the display() we wrote above, we can just say:
    out.println(names);
    

Examples

Consider the following two methods. Both of these methods reverse the order of the elements of an ArrayList<String>, and yet they are quite different.

public static ArrayList<Double> reverse1(ArrayList<Double> data) {
  ArrayList<Double> temp = new ArrayList<>();
  for(int i=0; i<data.size(); i++){
    temp.add(data.get(i));
  }
  return temp;
}

public static ArrayList<Double> reverse2(ArrayList<Double> data) {
  for(int i=0; i<data.size()/2; i++){
    int j = data.size()-1-i;
    Double temp = data.get(i);
    data.set(i, data.get(j));
    data.set(j, temp);
  }
  return data;
}
  • Here we made use of the Double wrapper class instead of double because ArrayLists cannot store primitive types.
  • Although these methods are doing the same thing as the methods with the same names in the array tutorial, the syntax is different because we now are sending messages to the ArrayList object instead of communicating directly with the array.
  • Do you see how these two methods differ in functionality?

Last modified: Monday, 15-Feb-2016 23:33:28 CST