Java 集合 获取LinkedList中元素最后一次出现的索引

编程以找出LinkedList中最后一个元素出现的索引。

程序

import java.util.LinkedList;
class LinkedListExample {

  public static void main(String[] args) {

     // create a LinkedList
     LinkedList<String> list = new LinkedList<String>();

     // Add elements
     list.add("AA");
     list.add("BB");
     list.add("CC");
     list.add("AA");
     list.add("DD");
     list.add("AA");
     list.add("EE");

     // Display LinkedList elements
     System.out.println("LinkedList elements: "+list);

     // get the index of last occurrence of element "AA"
     /* public int lastIndexOf(Object o): Returns the index 
      * of the last occurrence of the specified element in 
      * this list, or -1 if this list does not contain the 
      * element. 
      */
     System.out.println("LastIndex of AA:"+list.lastIndexOf("AA"));

     // get the index of last occurrence of element "ZZ"
     /* Note: The element ZZ does not exist in the list so
      * the method lastIndexOf would return -1 for it.
      */
     System.out.println("LastIndex of ZZ:"+list.lastIndexOf("ZZ"));
  }
}

输出:

LinkedList elements: [AA, BB, CC, AA, DD, AA, EE]
LastIndex of AA:5
LastIndex of ZZ:-1

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程