写一个方法传递List对象
import java.util.*;
..................
static int i;
public static void Test(List a){
a.add(1, "x"); // Add at location 1
a.add("x"); // Add at end
addAll(fill(new ArrayList()));
a.addAll(3, fill(new ArrayList()));
.........................
i = a.indexOf("1"); // Tell index of object
i = [color=red]a.indexOf("1", 2);[/color]
[color=red] i = a.lastIndexOf("1", 2 );[/color]
..................................
// Remove elements in this range:
[color=red]a.removeRange(0, 2);[/color]
.................................
}
为什么红色这两处会报错?
第一处报:
The method indexOf(Object) in the type List is not applicable for the arguments (String, int)
第二处报:
The method lastIndexOf(Object) in the type List is not applicable for the arguments (String, int)
第三处报:
The method removeRange(int, int) is undefined for the type List
小子我真的找不到原因了,请求各位帮忙。
谢谢~
[quote]我想在这个数组列表里面从指定位置开始查找一个元素第一次出现的位置,这样该用哪个方法查找?[/quote]
List里面没有提供你要的方法,你可以自己实现,很简单,从你指定的位置通过List的subList方法截断产生新的List,然后在新的List上调用indexOf方法,这样就能返回你要找的元素的位置,最后把这个值加上之前截断的位置就是原来List里面该元素的位置了。
大哥,list没有这些方法,你的那几个方法都是String的
童鞋,英文有待提高呀。
他没有这个方法,你硬是调用了这个方法,就报错了啊。
list没有这几个方法,当然报错了,真是粗心啊。
[quote]
那为什么i = a.indexOf("1")
i = a.lastIndexOf("1")
这两句能通过编译呢?
[/quote]
List接口中存在这样的方法,为何不能编译通过呢?看看API就知道了啊。
[quote]
那为什么i = a.indexOf("1")
i = a.lastIndexOf("1")
这两句能通过编译呢?
[/quote]
这两句能通过,是因为List类上面定义了这两个方法!你看看List的API doc,看看indexOf和 lastIndexOf这两个方法定义的参数,你的调用需要合乎定义。
[quote]
我是这样调用的,
Test(fill(new linkedList()));
[/quote]
fill方法定义在哪里?