I have an Arraylist of integers. My requirement is to determine if the arraylist HAS an element existing at the specified index.If YES, then a value should be set to that in
The other answers already provide information about using indexOf methods available in the list. However, just to add some info about difference between "add" and "set" in the ArrayList in java.
From the javadocs -
add(index, value)
method - Inserts the specified element at the specified position in this list. Shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices).
set(index, value)
- Replaces the element at the specified position in this list with the specified element.
So using add() instead of set() increases your list size
as well. You should consider this as well whether you need this behaviour or not.