// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
import java.util.*;

public class ArrayVersusArrayList {
   private final static int N1 = 100;
   private final static int N2 = 50000;
   private final static int N3 = 10;
   private static void test(String s, WrapperFactory wf) {
      long t1 = System.currentTimeMillis();
      for (int i = 0; i < N1; i++) {
         Wrapper w = wf.getWrapper();
         for (int j = 0; j < N2; j++) {
            w.set(j, j);
         }
         for (int k = 0; k < N3; k++) {
            for (int j = 0; j < N2; j++) {
               if (w.get(j) != j) {
                  throw new RuntimeException("Ooops");
               }
            }
         }
      }
      long t2 = System.currentTimeMillis();
      System.out.println(s + ": " + (t2 - t1));
   }
   private static void testSimpeltArrayInline() {
      long t1 = System.currentTimeMillis();
      for (int i = 0; i < N1; i++) {
         int[] a = new int[1000];
         for (int j = 0; j < N2; j++) {
            if (j >= a.length) {
               int[] tmp = a;
               a = new int[tmp.length + 1000];
               System.arraycopy(tmp, 0, a, 0, tmp.length);
            }
            a[j] = j;
         }
         for (int k = 0; k < N3; k++) {
            for (int j = 0; j < N2; j++) {
               if (a[j] != j) {
                  throw new RuntimeException("Ooops");
               }
            }
         }
      }
      long t2 = System.currentTimeMillis();
      System.out.println("simpelt array inline: " + (t2 - t1));
   }
   private static void testObjectArrayInline() {
      long t1 = System.currentTimeMillis();
      for (int i = 0; i < N1; i++) {
         Integer[] a = new Integer[1000];
         for (int j = 0; j < N2; j++) {
            if (j >= a.length) {
               Integer[] tmp = a;
               a = new Integer[tmp.length + 1000];
               System.arraycopy(tmp, 0, a, 0, tmp.length);
            }
            a[j] = new Integer(j);
         }
         for (int k = 0; k < N3; k++) {
            for (int j = 0; j < N2; j++) {
               if (a[j].intValue() != j) {
                  throw new RuntimeException("Ooops");
               }
            }
         }
      }
      long t2 = System.currentTimeMillis();
      System.out.println("object array inline: " + (t2 - t1));
   }
   private static void testArrayListInline() {
      long t1 = System.currentTimeMillis();
      for (int i = 0; i < N1; i++) {
         ArrayList list = new ArrayList();
         for (int j = 0; j < N2; j++) {
            list.add(j, new Integer(j));
         }
         for (int k = 0; k < N3; k++) {
            for (int j = 0; j < N2; j++) {
               if (((Integer) list.get(j)).intValue() != j) {
                  throw new RuntimeException("Ooops");
               }
            }
         }
      }
      long t2 = System.currentTimeMillis();
      System.out.println("ArrayList inline: " + (t2 - t1));
   }
   private static void testArrayListArrayInline() {
      long t1 = System.currentTimeMillis();
      for (int i = 0; i < N1; i++) {
         ArrayList list = new ArrayList();
         for (int j = 0; j < N2; j++) {
            int[] v2 = new int[1];
            v2[0] = j;
            list.add(j, v2);
         }
         for (int k = 0; k < N3; k++) {
            for (int j = 0; j < N2; j++) {
               if (((int[]) list.get(j))[0] != j) {
                  throw new RuntimeException("Ooops");
               }
            }
         }
      }
      long t2 = System.currentTimeMillis();
      System.out.println("ArrayList array inline: " + (t2 - t1));
   }
   public static void main(String[] args) {
      test("simpelt array", new SimpleArrayWrapperFactory());
      test("object array", new ObjectArrayWrapperFactory());
      test("ArrayList", new ArrayListWrapperFactory());
      test("ArrayList array", new ArrayListArrayWrapperFactory());
      testSimpeltArrayInline();
      testObjectArrayInline();
      testArrayListInline();
      testArrayListArrayInline();
   }
}

interface WrapperFactory {
   public Wrapper getWrapper();
}

class SimpleArrayWrapperFactory implements WrapperFactory {
   public Wrapper getWrapper() {
      return new SimpleArrayWrapper();
   }
}

class ObjectArrayWrapperFactory implements WrapperFactory {
   public Wrapper getWrapper() {
      return new ObjectArrayWrapper();
   }
}

class ArrayListWrapperFactory implements WrapperFactory {
   public Wrapper getWrapper() {
      return new ArrayListWrapper();
   }
}

class ArrayListArrayWrapperFactory implements WrapperFactory {
   public Wrapper getWrapper() {
      return new ArrayListArrayWrapper();
   }
}

interface Wrapper {
   public void set(int ix, int v);
   public int get(int ix);
}

class SimpleArrayWrapper implements Wrapper {
   private int[] a;
   public SimpleArrayWrapper() {
      a = new int[1000];
   }
   public void set(int ix, int v) {
      if (ix >= a.length) {
         int[] tmp = a;
         a = new int[tmp.length + 1000];
         System.arraycopy(tmp, 0, a, 0, tmp.length);
      }
      a[ix] = v;
   }
   public int get(int ix) {
      return a[ix];
   }
}

class ObjectArrayWrapper implements Wrapper {
   private Integer[] a;
   public ObjectArrayWrapper() {
      a = new Integer[1000];
   }
   public void set(int ix, int v) {
      if (ix >= a.length) {
         Integer[] tmp = a;
         a = new Integer[tmp.length + 1000];
         System.arraycopy(tmp, 0, a, 0, tmp.length);
      }
      a[ix] = new Integer(v);
   }
   public int get(int ix) {
      return a[ix].intValue();
   }
}

class ArrayListWrapper implements Wrapper {
   private ArrayList list;
   public ArrayListWrapper() {
      list = new ArrayList();
   }
   public void set(int ix, int v) {
      list.add(ix, new Integer(v));
   }
   public int get(int ix) {
      return ((Integer) list.get(ix)).intValue();
   }
}

class ArrayListArrayWrapper implements Wrapper {
   private ArrayList list;
   public ArrayListArrayWrapper() {
      list = new ArrayList();
   }
   public void set(int ix, int v) {
      int[] v2 = new int[1];
      v2[0] = v;
      list.add(ix, v2);
   }
   public int get(int ix) {
      return ((int[]) list.get(ix))[0];
   }
}
