📌  相关文章
📜  从Java中的ArrayList中获取第一个和最后一个元素

📅  最后修改于: 2022-05-13 01:54:59.117000             🧑  作者: Mango

从Java中的ArrayList中获取第一个和最后一个元素

给定一个数组列表,找到它的第一个和最后一个元素。

例子:

Input : aList = {10, 30, 20, 14, 2}
Output : First = 10, Last = 2

Input : aList = {10, 30, 40, 50, 60}
Output : First = 10, Last = 60

最后一个元素位于索引大小 - 1 处,第一个元素存储在索引 0 处。如果我们知道如何获取 ArrayList 的大小,那么我们可以轻松获取这两个值。但请记住,您需要对 ArrayList 使用 size() 方法,而不是 length,length 用于获取数组的长度。

在 ArrayList 中查找第一个和最后一个:

// java program print first and last element of a List
import java.util.ArrayList;
import java.util.List;
public class FindFirstLast {
    public static void getFirstLat(List list)
    {
  
        // Displaying ArrayList elements
        System.out.println("ArrayList contains: " + list);
  
        // Logic to get the last element from ArrayList
        if (list != null && !list.isEmpty()) {
            System.out.println("First element is: "
                               + list.get(0));
            System.out.println("Last element is: "
                               + list.get(list.size() - 1));
            return;
        }
    }
  
    public static void main(String[] args)
    {
        /* Creating ArrayList of Integer and adding
          elements to it */
        List al = new ArrayList();
        al.add(3);
        al.add(1);
        al.add(4);
        al.add(5);
        al.add(2);
  
        getFirstLat(al);
    }
}
输出:
ArrayList contains: [3, 1, 4, 5, 2]
First element is: 3
Last element is: 2

应用:第一个元素是你的最低元素,最后一个元素是你的最高元素,如果升序排列,相反,如果 List 是降序排列,第一个元素是最大值,最后一个元素是最小值。

// java program print Maximum and Minimum Value of a
// sorted List, List may be increasing or decreasing order
import java.util.ArrayList;
import java.util.List;
public class FindFirstLast {
    // function find and print Maximum and Minimum value
    public static void getFirstLat(List list)
    {
  
        // Displaying ArrayList elements
        System.out.println("ArrayList contains: " + list);
  
        // Logic to get the last element from ArrayList
        if (list != null && !list.isEmpty()) {
            if (list.get(0) < list.get(list.size() - 1)) {
  
                // if list in increasing order
                System.out.println("Minimum Value: "
                                   + list.get(0));
                System.out.println("Maximum Value: "
                           + list.get(list.size() - 1));
                return;
            }
  
            else {
  
                // if list in decreasing order
                System.out.println("Minimum Value: "
                            + list.get(list.size() - 1));
                System.out.println("Maximum Value: "
                                   + list.get(0));
                return;
            }
        }
    }
  
    public static void main(String[] args)
    {
        /* Creating ArrayList of Integer and adding
            elements to it */
        List al = new ArrayList();
        al.add(5);
        al.add(4);
        al.add(3);
        al.add(2);
        al.add(1);
  
        getFirstLat(al);
    }
}
输出:
ArrayList contains: [5, 4, 3, 2, 1]
Minimum Value: 1
Maximum Value: 5