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


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));
4

5 に答える 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.

于 2012-05-21T06:17:46.153 に答える
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...

于 2012-05-21T06:17:12.120 に答える
3

add(double, int) に適したメソッドが見つかりません...

エラーの説明は、add メソッドに 2 つの引数を追加しようとしていることを示しています。最初の引数はdoubleで、2 番目の引数は次のintようになりadd(double, int)ます。

Double次のように追加しようとするとどうなりますか。

list.add(new Double(5.5));
于 2012-05-21T06:16:57.877 に答える
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.

于 2012-05-21T06:18:00.667 に答える
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

于 2012-05-21T06:36:05.287 に答える