taylorialcom/ DataStructures

Introduction to ArrayLists

An ArrayList is a class that encapsulates an array and provides a number of convenient methods to simplify storing collections of like data.



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 = data.size()-1; i >= 0; 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;