× {{alert.msg}} Never ask again
Get notified about new tutorials RECEIVE NEW TUTORIALS

Adding to an Integer already in an ArrayList

James Jensen
Feb 10, 2015
<p>The <code>+=</code> operation is supposed to act on a variable--a local variable, a field, etc. And Integers are immutable, so you can't really change their value directly--5 will always be 5, and if you add 1 to it, you end up with a new number (6).</p> <p>So you need to first "get" the value that is at the given index, and then "set" the value at that index to the new number that comes from adding one to the original value:</p> <pre><code>arrayListOfIntegers.set(i, arrayListOfIntegers.get(i) + 1); </code></pre> <p>This tip was originally posted on <a href="http://stackoverflow.com/questions/25774874/Adding%20to%20an%20Integer%20already%20in%20an%20ArrayList/25774905">Stack Overflow</a>.</p>

Get New Tutorials Delivered to Your Inbox

New tutorials will be sent to your Inbox once a week.

comments powered by Disqus