260

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) {

}
oezi
  • 51,017
  • 10
  • 98
  • 115
user949902
  • 2,671
  • 2
  • 14
  • 4
  • 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. – GuiSim Mar 19 '14 at 14:55
  • 2
    @GuiSim the List interface isn't that long, you can easily read it whole at once in 5 mins... –  Jul 20 '14 at 11:13
  • 3
    I was using Eclipse's autocomplete, didn't look through the interface. – GuiSim Jul 22 '14 at 14:53

6 Answers6

434

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

TotoroTotoro
  • 17,524
  • 4
  • 45
  • 76
  • 3
    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 ? – Android Killer Mar 17 '13 at 17:20
  • 9
    @AndroidKiller It's static as `ArrayList.set();` but when you call it on your own list, it's not: `myArrayList.set(int, E);` – whitfin Aug 25 '13 at 03:39
155

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

Android Killer
  • 18,174
  • 13
  • 67
  • 90
  • 1
    @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. – Android Killer Oct 08 '13 at 08:38
  • 1
    @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??? – Android Killer Oct 15 '13 at 04:45
  • 1
    @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 :) – Android Killer Oct 15 '13 at 17:40
  • 2
    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. –  Oct 16 '13 at 22:16
31

Use the set() method: see doc

arraylist.set(index,newvalue);
Gundu Bandgar
  • 2,593
  • 1
  • 17
  • 21
Burt Beckwith
  • 75,342
  • 5
  • 143
  • 156
9

Use ArrayList.set

Oceanic
  • 1,418
  • 2
  • 12
  • 19
7
public void setItem(List<Item> dataEntity, Item item) {
    int itemIndex = dataEntity.indexOf(item);
    if (itemIndex != -1) {
        dataEntity.set(itemIndex, item);
    }
}
Khaled Lela
  • 7,831
  • 6
  • 45
  • 73
  • 2
    This will only work if the new item is equal to the old item in terms of the equals() method, right? – spikemanuk May 20 '16 at 07:08
  • @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} – Aniruddha Jagtap Apr 17 '19 at 09:53
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")
Dinith
  • 839
  • 14
  • 22