EDIT: Now that you've clarified your post, it's obvious - if you want to call the overload which takes the index as well, you need to specify the index as the first argument.
// Not this:
list.add(43.6, 9);
// But this:
list.add(9, 43.6);
The signature is:
public void add(int index, E element)
... not the other way round.
Unable to reproduce. This works fine:
ArrayList<Object> list = new ArrayList<Object>();
list.add(5.5);
list.add(new Double(5.4));
If you're trying to add two values with a single call (i.e. you're passing two arguments) then that's not going to work. You need a single add
call per value, or call addAll
with another collection.
Is it possible you were trying to use a comma within the value, e.g.
list.add(5.234,1);
as a way of trying to add "five thousand two hundred and thirty four point one"? That would produce the error above, and it has nothing to do with ArrayList
. The format for numeric literals in Java is always to use .
as the decimal separator, and you can't include commas - although as of Java 7 you can use underscores for grouping. So you could write:
list.add(5_234.1); // Java 7 only
or
list.add(5234.1);
Of course, this may not be what you're doing at all... but it's hard to tell as you haven't included the code that doesn't work...