The following code is bug free. But, it fine so long as if (i == a.length - 1 || a[i] != a[i + 1])
condition maintains its order. Swapping the if conditions to if(a[i] != a[i + 1] || i == a.length - 1)
would throw an exception. Is preventing an exception by short circuiting, an expectable coding standard Or is there some theory / design pattern warning against such coding practices ? If so please advise with links / materials.
public static void eliminateDuplicate(int[] a) {
if (a == null) {
throw new NullPointerException();
}
List<Integer> list = new ArrayList<Integer>();
for (int i = 0; i < a.length; i++) {
if (i == a.length - 1 || a[i] != a[i + 1]) {
System.out.println("now what : " + a[i]);
list.add(i);
}
}
}