How to perform Arithmetic with ArrayList values

learning2code :

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 !

gmanrocks :

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.

edited at
0

Comments

0 comments
Login to comment

Related

How to perform arithmetic on values and operators expressed as strings?

SQL: Perform arithmetic operations on values in a column

How to perform arithmetic operation in foreach java 8

How do I perform decimal arithmetic in Perl?

How does an AVR perform floating point Arithmetic

how to perform arithmetic operation to strings in java?

How to perform arithmetic operation on a date in Python?

How to perform arithmetic operation on a date in Python?

How to perform basic Excel arithmetic in Powershell?

How to perform arithmetic on the result of Collectors.Counting()?

How does Go perform arithmetic on constants?

How js Date() object perform arithmetic (+, - * / )?

How to perform ceiling-division in integer arithmetic?

how to perform arithmetic operation on string in MySQL?

How to perform arithmetic operations on the output of a shell command

How to perform arithmetic operation on Late Variable in Flutter?

Rust: How to perform arithmetic on a Vec of items

How to perform arithmetic on the individual digits of a number

awk: Perform arithmetic on subset of columns and print all columns with modified values

How to add values in arraylist of arraylist?

How To properly perform arithmetic operations over Two List/Array

How to perform arithmetic operations in a DataFrame groupBy aggregation in Spark?

How to perform image arithmetic for distinct size images in Matlab?

How to perform some arithmetic operations on binary bit number?

How to perform date arithmetic between nested and unnested dates in Elasticsearch?

How does a processor without an overflow flag perform signed arithmetic?

How to perform arithmetic operation on two seperate dataframes in Apache Spark?

How can I perform arithmetic operation taking a integer and string as input

How to perform all possible combinations of arithmetic operations on 3 integers?