1

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 !

4 Answers 4

1

You are using the ArrayList.get() method which is returning an integer and when you add that value to an integer it gives error, which is rightly so. Now you have to use get() method in conjunction with set() method like this:

//index to change, so in future you don't need to 
//change whole code just change value of 'i'
int i = 0; 
snakex.set(i, snakex.get(i)+5);  //first calls the 'get()' method and then sets that value

For more on ArrayList click here

Sign up to request clarification or add additional context in comments.

Comments

0

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.

2 Comments

It is still giving me the same error "The left-hand side of an assignment must be a variable". But then I only wrote a '+' sign and it worked! Thanks
@learning2code it is a'ight. I didn't know how to at first either. Good luck though bro. CodingIsLife
0

You can use the set() method of java.util.ArrayList class to replace an existing element of ArrayList in Java. The set(int index, E element) method takes two parameters, first is the index of an element you want to replace and second is the new value you want to insert.

i.e.

snakex.set(0, snakex.get(0)+5)

Comments

0

Here I am giving some example for arrayList:

ArrayList<Integer> snakex = new ArrayList<Integer>();

        snakex.add(630);
        snakex.add(640);
        snakex.add(650);
        snakex.add(660);

        for (int index = 0; index < snakex.size(); index++) {
            Integer item=snakex.get(index);//getting item for position
            snakex.set(index,(item+5)); // It is adding 5 with each item and storing tht position
        }

        // print each item using for each
        for (Integer item:snakex) {
            System.out.println(item+" ");
        }

        // delete item which value is 640
        for (int index = 0; index < snakex.size(); index++) {
            Integer item=snakex.get(index);//getting item for position

            if(item==640){
                snakex.remove(item);
            }
        }

For more info please visit here

Hope this will help you.

Thanks :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.