1

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 3

2

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));
Sign up to request clarification or add additional context in comments.

8 Comments

list doesn't have set method.
@adashr See this
@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.
Ok I take back the downvote. But the OP wanted to insert, not replace.
@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.
|
1

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

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.