how should i insert a double into a specific part of ArrayList? for example we have a ArrayList with ten doubles each one with the 0 value we want to make the sixth place 10, how should we do that?
3 Answers
Use ArrayList.set() method:
public E set(int index,
E element)
Replaces the element at the specified position in this list
with the specified element.
For example:
list.set(5, new Double(10));
8 Comments
adarshr
list doesn't have
set method.Jigar Joshi
@adashr See this
hmjd
@adarshr, yes i does:
public E set(int index, E element)Replaces the element at the specified position in this list with the specified element. adarshr
Ok I take back the downvote. But the OP wanted to insert, not replace.
hmjd
@adarshr, I think the OP does want to replace as all it is stated that there is already an ArrayList with ten doubles, all with value zero.
|
Just used the indexed add.
list.add(6, 10D);
EDIT:
But if you want to replace the value at the specified index (instead of inserting a new one), I suggesst you follow @hmjd's solution.
Comments
See the documentation: http://docs.oracle.com/javase/1.4.2/docs/api/java/util/ArrayList.html
void add(int index, Object element);
Inserts the specified element at the specified position in this list.