267

I need help with this java please. I created an ArrayList of bulbs, and I'm trying to replace a bulb at specific index with another bulb. So with the following heading, how do I proceed?

public void replaceBulb(int index, Bulbs theBulb) {

}
3
  • 13
    For what it's worth, I was looking at the List interface for "replace", "put" or "insert". I didn't think of looking for set. Commented Mar 19, 2014 at 14:55
  • 2
    @GuiSim the List interface isn't that long, you can easily read it whole at once in 5 mins... Commented Jul 20, 2014 at 11:13
  • 3
    I was using Eclipse's autocomplete, didn't look through the interface. Commented Jul 22, 2014 at 14:53

6 Answers 6

446

Check out the set(int index, E element) method in the List interface

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

2 Comments

u r right.But u may think to edit it because ur answer leads to the fact like set() method is a static method which is not, is it ?
@AndroidKiller It's static as ArrayList.set(); but when you call it on your own list, it's not: myArrayList.set(int, E);
160

You can replace the items at specific position using set method of ArrayList as below:

list.set( your_index, your_item );

But the element should be present at the index you are passing inside set() method else it will throw exception.

Also you can check oracle doc here

4 Comments

@vaxquis ok sir you please write java 8 and release it. we are here to use it. ;). In SO, answer should be always according to question, so please read the question first. And what is the problem if it returns the previous value, if you want that value then you can use else leave it. It is replacing the old value and that is it.
@vaxquis did u read my comment before. Clearly mentioned answer should be always according to question. The person who asked question, he has mentioned he wants to replace the element so that means he has the arraylist with elements in it. now clear Sir???
@vaxquis The things you consider, a newbie will know this, so these issues does not make any sense. Still i have edited to make u happy :)
Being a newbie means that somebody DOESN'T KNOW how to code something properly. It is ALWAYS wrong to provide invalid code and there is no excuse to it, because it can and usually will bring confusion to beginners. Providing a correct and self contained code example is what StackOverflow is about. Thank you for the improvement on your answer.
32

Use the set() method: see doc

arraylist.set(index,newvalue);

Comments

9

Use ArrayList.set

Comments

7
public void setItem(List<Item> dataEntity, Item item) {
    int itemIndex = dataEntity.indexOf(item);
    if (itemIndex != -1) {
        dataEntity.set(itemIndex, item);
    }
}

2 Comments

This will only work if the new item is equal to the old item in terms of the equals() method, right?
@spikemanuk, it depends upon the implementation of 'equals()' method in 'Item'. If equality solely depends on 'id' of the 'Item' then this code can be used to replace 'Item:{id=1, name=London} by 'Item:{id=1, name=Paris}
1

Lets get array list as ArrayList and new value as value all you need to do is pass the parameters to .set method. ArrayList.set(index,value)

Ex -

ArrayList.set(10,"new value or object")

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.