I'm a beginner at using ArrayList and I want to add a number to a value stored in an element. Basically I want to do arlist(0) += number. here is my code (I've only pasted the relevant parts).
ArrayList<Integer> snakex = new ArrayList<Integer>();
snakex.add(630);
I'm not sure how to go on from here. I've tried:
snakex.get(0) += 5;
Doing this I get the error "The left-hand side of an assignment must be a variable".
How would I be able to change the value of snakex(0) from 630 to 635?.
Thank you !
What you are basically doing is:
snakex.get(0) += 5 -> 630 += 5 -> 635;
It does not know what to do from there. Instead do:
snakex.set(0, snakex.get(0) + 5)
The set method is defined by set(int index, Object o) . The get(int index) gets the value at the specified index. The set(int index, Object o) sets the value at the specified index to the object.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments