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
Check out the set(int index, E element)
method in the List interface
Use ArrayList.set
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.
Use the set()
method: see doc
arraylist.set(index,newvalue);
public void setItem(List<Item> dataEntity, Item item) {
int itemIndex = dataEntity.indexOf(item);
if (itemIndex != -1) {
dataEntity.set(itemIndex, item);
}
}