From the documentation http://docs.oracle.com/javase/7/docs/api/java/util/List.html
add(int index, E element)
Inserts the specified element at the specified position in this list (optional operation).
set(int index, E element)
Replaces the element at the specified position in this list with the specified element
(optional operation).
So no. They don't do the same thing. add adds. set replaces an existing element.
If there is no element a the index, set will return an error:
IndexOutOfBoundsException - if the index is out of range (index < 0 || index >= size())
You could, therefore, try them both:
try
{
list.set(index, obj);
}
catch(IndexOutOfBoundsException ex)
{
list.add(index, obj);
}
Try to set, and if it returns that specific error, add instead.