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 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了