/** * An IntList is a list of ints. */ public class IntList { private int[] values; private IntList(int[] values) { this.values = values; } public static IntList make(int... values){ return new IntList(values); } public final static IntList NIL = new IntList(new int[0]); /** * 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) { //make new array int[] array = new int[values.length + 1]; // copy old array in starting at position 1 for(int i = 1; i < array.length; i++) array[i] = values[i-1]; // put h at position 0 array[0] = h; return new IntList(array); } /** * Get our length. * @return our int length */ public int length() { return values.length; } /** * Print ourself to System.out. */ public void print() { System.out.print("["); for(int i = 0; i < values.length; i++){ if(i>0) System.out.print(","); System.out.print(values[i]); } System.out.println("]"); } }