小编Nan*_*747的帖子

在集合中,如何使用以下示例中的indexOf方法获取索引

    class Fruit{
      public String name;
      Fruit(String name){
        this.name = name;
        }
    }//end of Fruit

    class FruitList{
     public static void main(String [] arg5){
        List<Fruit> myFruitList = new ArrayList<Fruit>();
        Fruit banana = new Fruit("Banana"); 
    //I know how to get the index of this banana
        System.out.println("banana's index "+myFruitList.indexOf(banana));
 //But i'm not sure how can i get the indices for the following objects
        myFruitList.add(new Fruit("peach"));
        myFruitList.add(new Fruit("orange"));
        myFruitList.add(new Fruit("grapes"));
  }//end of main 

}//end of FruitList
Run Code Online (Sandbox Code Playgroud)

由于我添加到ArrayList的其他对象没有引用,我不太确定如何检索它们的索引.请帮忙,非常感谢.

java collections arraylist indexof

3
推荐指数
1
解决办法
141
查看次数

标签 统计

arraylist ×1

collections ×1

indexof ×1

java ×1