could you please help me understand this ternary operator
public static <E> void replace(List<E> list, E val, E newVal) {
for (ListIterator<E> it = list.listIterator(); it.hasNext(); )
if (val == null ? it.next() == null : val.equals(it.next()))
it.set(newVal);
}
i thought the part after ? is just a regular statement, so how is == being used here?
it.next() == null
is an expression,val.equals(it.next()))
is an expression. Ifval == null
, evaluate the former, else evaluate the latter. – Ismail Badawi Jul 24 '13 at 05:12