Best way to increment Integer in arrayList in Java

arraylist, integer, java

Solution

Maybe you need to use another structure of data?

LinkedList<AtomicInteger> ints = new LinkedList<AtomicInteger>();
ints.add(new AtomicInteger(5));
ints.add(new AtomicInteger(9));

ints.getLast().incrementAndGet();

Problem

What is the cleanest way to increment an Integer in an ArrayList? ``` ArrayList<Integer> ints = new ArrayList<>(); ints.add(5); ints.add(9); ``` What is the cleanest way to increment the last element? `ints.set(ints.size() - 1, ints.get(ints.size() - 1) + 1);` seems pretty ugly to me.

Original source