Java ArrayList replace at specific index

后端 未结 5 2122
说谎
说谎 2020-11-27 13:36

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

相关标签:
5条回答
  • 2020-11-27 14:06

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

    0 讨论(0)
  • 2020-11-27 14:12

    Use ArrayList.set

    0 讨论(0)
  • 2020-11-27 14:13

    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.

    0 讨论(0)
  • 2020-11-27 14:14

    Use the set() method: see doc

    arraylist.set(index,newvalue);
    
    0 讨论(0)
  • 2020-11-27 14:23
    public void setItem(List<Item> dataEntity, Item item) {
        int itemIndex = dataEntity.indexOf(item);
        if (itemIndex != -1) {
            dataEntity.set(itemIndex, item);
        }
    }
    
    0 讨论(0)
提交回复
热议问题