Java Java.util.LinkedList.get(), getFirst(), getLast()方法及应用

Java Java.util.LinkedList.get(), getFirst(), getLast()方法及应用

传统的方法是通过get()来 获取特定索引的元素 。尽管在LinkedList中,如果不进行完全的遍历,就不可能实现这个目标,但是这个方法也可以实现。有三种变体,本文将对其进行讨论,同时也会对异常情况进行讨论。

1. get(int index): 该方法返回列表中 指定位置 的元素。

声明:
public E get(int index)
参数 :
index : 要返回的元素的索引
返回值 :
该方法返回该列表中指定位置的元素。
异常 ***
**IndexOutOfBoundsException :
如果索引超出了范围。

// Java code to demonstrate the working
// of get(int index) in linked list
import java.util.*;
public class LinkedListget1 {
public static void main(String[] args)
    {
        // declaring a LinkedList
        LinkedList<String> list = new LinkedList<String>();
  
        // adding elements using add()
        list.add("Geeks");
        list.add("4");
        list.add("Geeks");
        list.add("8");
  
        // printing the whole list
        System.out.println("The elements in List are : " + list);
  
        // using get() to print element at index 3
        // prints 8
        System.out.println("Element at index 3 is : " + list.get(3));
    }
}

输出:

The elements in List are : [Geeks, 4, Geeks, 8]
Element at index 3 is : 8

2. getFirst() : 该方法返回该列表中的 第一个 元素。

声明:
public E getFirst()
返回值 :
该方法返回该列表中的第一个元素
异常 :
NoSuchElementException : 如果这个列表是空的

// Java code to demonstrate the working
// of getFirst() in linked list
import java.util.*;
public class LinkedListget2 {
public static void main(String[] args)
    {
        // declaring a LinkedList
        LinkedList<String> list = new LinkedList<String>();
  
        // adding elements using add()
        list.add("Geeks");
        list.add("4");
        list.add("Geeks");
        list.add("8");
  
        // printing the whole list
        System.out.println("The elements in List are : " + list);
  
        // using get() to print element at first index
        // prints "Geeks"
        System.out.println("Element at 1st index is : " + list.getFirst());
    }
}

输出:

The elements in List are : [Geeks, 4, Geeks, 8]
Element at 1st index is : Geeks

3. getLast() : 该方法返回该列表中的 最后一个 元素。

声明:
public E getLast()
返回值 :
该方法返回该列表中的最后一个元素
异常 :
NoSuchElementException : 如果这个列表是空的

// Java code to demonstrate the working
// of getLast() in linked list
import java.util.*;
public class LinkedListget3 {
public static void main(String[] args)
    {
        // declaring a LinkedList
        LinkedList<String> list = new LinkedList<String>();
  
        // adding elements using add()
        list.add("Geeks");
        list.add("4");
        list.add("Geeks");
        list.add("8");
  
        // printing the whole list
        System.out.println("The elements in List are : " + list);
  
        // using get() to print element at last index
        // prints "8"
        System.out.println("Element at last index is : " + list.getLast());
    }
}

输出:

The elements in List are : [Geeks, 4, Geeks, 8]
Element at last index is : 8

异常情况

1.IndexOutOfBoundException

// Java code to demonstrate the Exceptions
// of get()
import java.util.*;
public class LinkedListExcep1 {
public static void main(String[] args)
    {
        // declaring a LinkedList
        LinkedList<String> list = new LinkedList<String>();
  
        // adding elements using add()
        list.add("Geeks");
        list.add("4");
        list.add("Geeks");
        list.add("8");
  
        // Trying to get element at index 7
        // throws exception
        System.out.println("The element at index 7 is : " + list.get(7));
    }
}

输出:

No Output

运行时错误:

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 7, Size: 4
    at java.util.LinkedList.checkElementIndex(LinkedList.java:555)
    at java.util.LinkedList.get(LinkedList.java:476)
    at LinkedListExcep1.main(LinkedListExcep1.java:22)

2.NoSuchElementException

// Java code to demonstrate the Exceptions
// of getFirst()
import java.util.*;
public class LinkedListExcep2 {
public static void main(String[] args)
    {
        // declaring a LinkedList
        LinkedList<String> list = new LinkedList<String>();
  
        // Trying to get first element at index 7
        // throws exception
        System.out.println("The first element of list is : " + list.getFirst());
    }
}

输出:

No Output

运行时错误:

Exception in thread "main" java.util.NoSuchElementException
    at java.util.LinkedList.getFirst(LinkedList.java:244)
    at LinkedListExcep2.main(LinkedListExcep2.java:15)

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程