Table of Contents

List Collections

Arrays

int[] integers;
integers = new int[20];
Integer[] integers;
integers = new Integer[20];
for(int i=0; i<integers.length; ++i)
{
  integers[i] = new Integer(i);
}
public static void display(String[] names, PrintStream out)
{
  for(int i=0; i<names.length; ++i)
  {
    out.println(names[i]);
  }
}
String[] names = {"Peter", "Paul", "Mary-Anne"};
display(names, System.out);  // Note: System.out is a PrintStream object
public static void display(String name1, String name2, String name3, PrintStream out)
{
  out.println(names1);
  out.println(names2);
  out.println(names3);
}
  1. Create a new, larger, array
  2. Copy over all the primitive values/references in the old array
  3. Reassign the reference to the original array so that it now points to the new array
/**
  * Resizes an array of Strings
  * @param array Array to be resized
  * @param size Desired size for the array
  * @return A reference to the newly sized array
  */
public static String[] resize(String[] array, int size)
{
  String[] newArray = new String[size];
  // Since the size could be less than array.length, we need to make sure
  //  we only go up to the smaller of the two values.
  for(int i=0; i<array.length && i<size; ++i)
  {
    newArray[i] = array[i];
  }
  return newArray;
}

Interfaces

private class ButtonEventHandler implements ActionListener {
  public void actionPerformed(ActionEvent event)
  String word = new String("nonsense");
  CharSequence charSeq = word;
  word.toString();
  word.endsWith("sense");
  word.length();
  word.subSequence(1, 3);
  word.substring(1, 3);
  charSeq.toString();
  charSeq.endsWith("sense");  // ILLEGAL
  charSeq.length();
  charSeq.subSequence(1, 3);
  charSeq.substring(1, 3);    // ILLEGAL

Java Collections Framework

List Interface

public static void silly(List<String> words)
{
  if(!words.isEmpty())
  {
    String word = words.get(0);
    words.add(word.trim());
    words.set(words.size()-1, "I'm last");
  }
  for(String word : words)
  {
    System.out.println(word);
  }
}
List<String> words = new List<String>();

List Implementations

public class ArrayList<E> implements List<E>
{
  private E[] array;
 
  public boolean isEmpty()
  {
    return (null == array);
  }
 
  public void clear()
  {
    array = null;
  }
 
  public E get(int index)
  {
    return array[index];
  }
 
  public void set(int index, E obj)
  {
    array[index] = obj;
  }
 
  public int size()
  {
    return array.length;
  }
 
  public boolean contains(E target)
  {
    boolean found = false;
    for(int i=0; !found && i<array.length; ++i)
    {
      found = target.equals(array[i]);
    }
    return found;
  }
 
  public boolean add(E obj)
  {
    E[] newArray = (E[])new Object[array.length+1];
    for(int i=0; i<array.length; ++i)
    {
      newArray[i] = array[i];
    }
    newArray[newArray.length-1] = obj;
    array = newArray;
    return true;
  }
} // end of ArrayList class
List<String> words = new ArrayList<String>();
// do a bunch of .add calls to populate the list
silly(words);
 
words = new LinkedList<String>();
// do a bunch of .add calls to populate the list
silly(words);
1) assuming your wildest dreams are about easily adding elements to a collection of items