0


I'm learning how to use ArrayLists and decided to try making an ArrayList. I have found that I can do:

ArrayList<Object> list = new ArrayList<Object>();
list.add("Hi");
list.add(new Integer(5), 9);

(And I realize that I can just add an int, and it will auto-box it)
The problem is that I cannot put a double or Double inside of the ArrayList at a specified index, which I can do with Integer. I've tried like this:

list.add(new Double(4)); // I just redid it, and this one works. 
list.add(45.6); // So does this one.
list.add(new Double(4), 6); // it's this one that doesn't.
list.add(43.6, 9); // doesn't work either

So Doubles do fit in an ArrayList, but you can't specify the index they should be at. If you try, the error that results is:

no suitable method found for add(double, int)
    method java.util.ArrayList.add(int,java.lang.Object) is not applicable
      (actual argument double cannot be converted to int by method invocation conversion)
    method java.util.ArrayList.add(java.lang.Object) is not applicable
      (actual and formal argument lists differ in length)

Why won't it allow a double (or a String) at a specified index, yet it allows an Integer?
Thanks, -AJ

8
  • 8
    How are you adding Double, show us Commented May 21, 2012 at 6:14
  • Looking at the error it looks like you are passing a double and an int into the add method, and ArrayList does not contain an overloaded method that accepts this combination of objects. Commented May 21, 2012 at 6:16
  • Don't you use any editor (IDE)? Commented May 21, 2012 at 6:20
  • 2
    @AlexG: You've got the arguments the wrong way round, that's all. Read the Javadoc carefully. Commented May 21, 2012 at 6:28
  • 2
    @AlexG: Note that if you'd posted the code which wasn't working (and explained what you were trying to do) right from the start, you wouldn't have wasted everyone's time trying to guess what was going on. Please read tinyurl.com/so-hints and bear it in mind for future questions. Commented May 21, 2012 at 6:31

5 Answers 5

6

Take a closer look at the error being given. It's saying you are invoking the add method with two arguments. In this particular case you are passing in a double and an int. That method clearly doesn't exist, even with the generic type erasure.

It seems like you really did intend to call the two argument add method. In that case, you have the arguments reversed. The first argument must be the index (position) in the list, while the second argument must be the element being added. Your example should then be:

list.add(6, new Double(4));

This would be why adding an integer at a specific position worked for you. According to the type-checker, it is technically correct. However, since the first argument was an integer, it interpreted it as the index, whereas you were expecting it to be added to the list.

Sign up to request clarification or add additional context in comments.

2 Comments

Ok, I can't believe I got those flipped around. I thought it was list.add(object to be added, index). Thank you.
@AlexG: When it didn't work, did you consider consulting the documentation before asking a question? When anything unexpected happens, the first thing I suspect is that my memory is incorrect...
3

no suitable method found for add(double, int)...

Your error description tells that you are trying to add two arguments into your add method, the first one is double and the second one is int like this, add(double, int).

What happens when you try to add a Double like this:

list.add(new Double(5.5));

Comments

3

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...

5 Comments

Sir, you might want to just fix the , and . in list.add(5.234,1). :-)
@KazekageGaara: No, it's exactly as I wanted it. That's what's required to produce the error. Note that some cultures use , as the decimal separator and . as the grouping separator.
Oh. I didn't know that , is used as decimal separator and . as grouping separator in some cultures as you mentioned.
No, the comma is intentional. I know I can add an Integer at a specified index in the ArrayList through .add(new Integer(5), 7); this adds 5 at the seventh index. But i don't know why it doesn't (or shouldn't) work for Doubles/Strings.
@AlexG: No, that's not what that call does. That adds 7 at the 5th index. Read the Javadoc (and my edited answer). And next time, ask a clearer question to start with.
0

The code you've posted does not match the error you get. no suitable method found for add(double, int) suggests that you are trying to use two argumenet method instead of one arg.

Comments

0

here u are using the wrong method,

there are two add methods

    ArrayList < Object > listObject = new ArrayList < Object > ( );

    listObject.add ( "Hi" );
    **listObject.add ( new Integer ( 5 ) );
    listObject.add ( 1,new Double ( 5.0 ) );**
    System.out.println (listObject);

in first add method will add the object at the end of the list. in second method the object will be added to the specified location,(i.e at 1st position in the given list)

as per your exception message no suitable method found for add(double, int) method java.util.ArrayList.add(int,java.lang.Object) is not applicable (actual argument double cannot be converted to int by method invocation conversion) method java.util.ArrayList.add(java.lang.Object) is not applicable (actual and formal argument lists differ in length)

you are giving double value in the place of position for the value in list. which cant be. so it should be always int and value can be any object

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.