/** * An IntList is a list of ints. */ public class IntList { private int[] elements; /** * Return an IntList containing the given elements. * @param elements the ints to go in the list */ public static IntList make(int... args){ return new IntList(args); } private IntList(int[] elements){ this.elements = elements; } /** * Return an empty list. */ public static IntList getEmpty(){ return make(); } /** * Cons the given element h onto us and return the * resulting IntList. * @param h the head int for the new list * @return the IntList with head h and us for a tail */ public IntList cons (int h) { int[] newArray = new int[elements.length + 1]; for(int i = 0; i < elements.length; i++){ newArray[i+1] = elements[i]; } newArray[0] = h; return new IntList(newArray); } /** * Get our length. * @return our int length */ public int length() { return elements.length; } /** * Print ourself to System.out. */ public void print() { System.out.print("["); for(int i = 0; i < elements.length; i++){ if(i != 0){ System.out.print(","); } System.out.print(elements[i]); } System.out.println("]"); } }