// 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.ArrayList;
import java.util.List;

public class ListVsArray {
    private static long getMem() {
        Runtime rt = Runtime.getRuntime();
        rt.gc();
        return rt.totalMemory() - rt.freeMemory();
    }
    private static final int N = 10000000;
    private static void testArray() {
        long m1 = getMem();
        Integer[] a = new Integer[N];
        for(int i = 0; i < N; i++) a[i] = i;
        long m2 = getMem();
        System.out.println(a[N/2]);
        System.out.println("A: " + (m2 - m1));
        a = null;
    }
    private static void testList() {
        long m3 = getMem();
        List<Integer> lst = new ArrayList<Integer>(N);
        for(int i = 0; i < N; i++) lst.add(i);
        long m4 = getMem();
        System.out.println(lst.get(N/2));
        System.out.println("L: " + (m4 - m3));
        lst = null;
    }
    private static void testSimpleArray() {
        long m1 = getMem();
        int[] a = new int[N];
        for(int i = 0; i < N; i++) a[i] = i;
        long m2 = getMem();
        System.out.println(a[N/2]);
        System.out.println("S: " + (m2 - m1));
        a = null;
    }
    public static void main(String[] args) {
        testArray();
        testList();
        testSimpleArray();
        testArray();
        testList();
        testSimpleArray();
        testArray();
        testList();
        testSimpleArray();
    }
}
