Jun_calior 2011-08-17 16:52
浏览 314
已采纳

List当中indexOf(String, int), removeRange(int, int), lastIndexOf(String, int)的用法

写一个方法传递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

小子我真的找不到原因了,请求各位帮忙。
谢谢~

  • 写回答

7条回答 默认 最新

  • myali88 2011-08-18 11:06
    关注

    [quote]我想在这个数组列表里面从指定位置开始查找一个元素第一次出现的位置,这样该用哪个方法查找?[/quote]
    List里面没有提供你要的方法,你可以自己实现,很简单,从你指定的位置通过List的subList方法截断产生新的List,然后在新的List上调用indexOf方法,这样就能返回你要找的元素的位置,最后把这个值加上之前截断的位置就是原来List里面该元素的位置了。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(6条)

报告相同问题?

悬赏问题

  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建