Java ArrayList overwrite

arraylist, java

Solution

The JavaDoc says...

set public E set(int index, E element) Replaces the element at the specified position in this list with the specified element. Specified by: set in interface List Overrides: set in class AbstractList Parameters: index - index of the element to replace element - element to be stored at the specified position Returns: the element previously at the specified position Throws: IndexOutOfBoundsException - if the index is out of range (index < 0 || index >= size())

So, basically, you can simply override the value at a specific location...

You may also want to take a look at Collections

Problem

A simple question: I store some values at an ArrayList at specific positions (indexes). These values are frequently updated by the code. My question is, in order to keep the ArrayList updated, it is sufficient to add the new value at the proper index (i.e. this action overwrites the older value stored there?) or do I have to remove first that value stored in that position of the ArrayList and then add the new value at this (now empty) position in the ArrayList?

Original source