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


set_theme does not work in module pyroCMS 2.1

I create a custom theme with name "uni" in addons/default/themes. Ok this work because i can see it in CP.

Next i create a custom module with code :

class Server extends Public_Controller
{
public function __construct()
{
parent::__construct();

}
public function index()
{
$this->template->title($this->module_details['name'])->set_theme('uni')->build('test');

}
}

But when i browser this module, theme still is active theme on CP, so set_theme in module controller does not work.

Any way to make module using custom theme instead of change the active theme in CP?

4

5 回答 5

6

仔细看看给出的错误。这就是说您正在add使用两个参数调用该方法。在这种特殊情况下,您将传入 adouble和 an int。即使使用泛型类型擦除,该方法显然也不存在。

看来您确实打算调用两个参数add方法。在这种情况下,你的论点颠倒了。第一个参数必须是列表中的索引(位置),而第二个参数必须是要添加的元素。你的例子应该是:

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

这就是为什么在特定位置添加整数对您有用。根据类型检查器,它在技术上是正确的。然而,由于第一个参数是一个整数,它将它解释为索引,而您期望它被添加到列表中。

于 2012-05-21T06:17:46.153 回答
3

没有找到适合 add(double, int) 的方法...

您的错误描述告诉您正在尝试将两个参数添加到您的 add 方法中,第一个是double,第二个是int这样的,add(double, int).

当您尝试添加Double这样的内容时会发生什么:

list.add(new Double(5.5));
于 2012-05-21T06:16:57.877 回答
3

编辑:既然您已经澄清了您的帖子,很明显 - 如果您想调用也采用索引的重载,您需要将索引指定为第一个参数。

// Not this:
list.add(43.6, 9);
// But this:
list.add(9, 43.6);

签名是:

public void add(int index, E element)

......不是反过来。


无法重现。这工作正常:

ArrayList<Object> list = new ArrayList<Object>();
list.add(5.5);
list.add(new Double(5.4));

如果您尝试通过一次调用添加两个值(即您正在传递两个参数),那么这是行不通的。您需要对add每个值进行一次调用,或者addAll使用另一个集合进行调用。

您是否有可能尝试在值中使用逗号,例如

list.add(5.234,1);

作为一种尝试添加“五千二百三十四点一”的方式?那会产生上面的错误,和 . 无关ArrayList。Java 中数字文字的格式始终用作.小数分隔符,并且不能包含逗号 - 尽管从 Java 7 开始,您可以使用下划线进行分组。所以你可以写:

list.add(5_234.1); // Java 7 only

或者

list.add(5234.1);

当然,这可能根本不是你正在做的......但很难说,因为你没有包含不起作用的代码......

于 2012-05-21T06:17:12.120 回答
0

您发布的代码与您收到的错误不符。没有为add(double, int)找到合适的方法表明您正在尝试使用两个 argumenet 方法而不是一个 arg。

于 2012-05-21T06:18:00.667 回答
0

在这里你使用了错误的方法,

有两种添加方法

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

在第一个 add 方法中,将在列表末尾添加对象。在第二种方法中,对象将被添加到指定位置,(即在给定列表中的第一个位置)

根据您的异常消息 ,找不到适合 add(double, int) 方法的方法 java.util.ArrayList.add(int,java.lang.Object) 不适用(实际参数 double 不能通过方法调用转换转换为 int )方法 java.util.ArrayList.add(java.lang.Object) 不适用(实际参数列表和形式参数列表的长度不同)

您在列表中的值的位置给出双倍值。这不可能。所以它应该总是 int 并且 value 可以是任何对象

于 2012-05-21T06:36:05.287 回答