0

So here I have an array of numbers and I want to change the value of a specific index only.

ArrayList <Integer> numbers = new ArrayList <> ();
numbers.add(5);
numbers.add(10);
numbers.add(20);

I was trying to do something like userNumbers.get(1 (* 3)); where 1 should be the index and I'd multiply the value in that index by 3 therefore will result as 30 since 10 is the 1st index. Tried searching up but no luck for me!

1
  • 4
    You need to do a get and a set, numbers.set(1, numbers.get(1) * 3) Commented Jun 20, 2018 at 2:16

3 Answers 3

6

Refer to the Java documentation whenever possible. This in particular.

numbers.set(1, numbers.get(1) * 3);
Sign up to request clarification or add additional context in comments.

Comments

2

Personally, I think it is because "Integer" is an immutable class, which means you cannot change the value of an immutable object with its member method.

If you put Integer, Long , Float ,Double, Boolean, Short, Byte, Character, String and other immutable classes in the list, you cannot change the value instantly.

But if you put customized objects in the list , you can change the value.

Demo code:

public class RRR {
    public static void main(String[] args) {
        ArrayList <Hi> hiList = new ArrayList <> ();
        Hi hi1 = new Hi("one");
        Hi hi2 = new Hi("two");
        Hi hi3 = new Hi("three");
        hiList.add(hi1);
        hiList.add(hi2);
        hiList.add(hi3);
        Hi hix = hiList.get(0);
        hix.setName("haha");
        System.out.println(hiList.get(0).getName());    // changed from "one" to "haha"
    }


}

class Hi {
    public Hi(String name) {
        this.name = name;
    }
    private String name;
    public void setName(String name) {
        this.name = name;
    }
    public String getName() {
        return this.name;
    }
}

See? The class Hi is not Immutable , you can change this value with "setName"

Back to the question, change this Integer object , you can:

  1. Copy the new value and origin values to a new list, if your list is not too large(not good).
  2. Delete the old element, then set the new value to the right index.(should consider thread safe problem)

Comments

1

I wrote a static method to make the changes, you could possibly add this method to the ArrayList class as well. This will multiply the element by 3 as you said you wanted in your post:

public static void main(String[] args) {
    ArrayList<Integer> numbers = new ArrayList<>();
    numbers.add(5);
    numbers.add(10);
    numbers.add(20);

    System.out.println(numbers.get(2)); //before

    changeList(numbers, 2);

    System.out.println(numbers.get(2)); //After!
}

//This method will take in the list, and the element number
static void changeList(ArrayList<Integer> arrayList, int elementNum) {
    int temp = arrayList.get(elementNum);
    arrayList.remove(elementNum); //remove it

    int newValue = temp * 3; //do anything you want here to the data
    arrayList.add(elementNum, newValue); //place the value back in the arrayList
}

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.