public static int safeGetElement(ArrayList<Integer> list, int index, int defaultValue) {
    try{
     list.get(index);
    }
    catch(IndexOutOfBoundsException e){
        return defaultValue;
    }
    return list.get(index);