java list最后一个元素_Java实现寻找列表List中最后一个元素

Java实现寻找列表List中最后一个元素

如何在一个元素列表List中找到最后一个元素?

import java.util.LinkedList;

import java.util.List;

import java.util.NoSuchElementException;

/**

* (*) Find the last element of a list.

*

* Check P01Test class for test cases related to this problem.

*/

public class P01 {

/*

You could solve this using many different approaches.

If you work with List interface then you can find the last element using List size as shown below.

*/

public static T last(List elements) {

int numberOfElements = elements.size();

return elements.get(numberOfElements - 1);

}

/*

Another way to solve this is by using LinkedList instead of a List.

LinkedList provides a builtin getLast method.

*/

public static T last(LinkedList elements) {

return elements.getLast();

}

/*

A functional approach could be to use recursion.  We call the function recusively with a sub list which ignores the 0th element.

When we reach the end i.e. size of the list is 1 then we return that element.

*/

public static T lastRecursive(List elements) {

if (elements == null || elements.isEmpty()) {

throw new NoSuchElementException();

}

if (elements.size() == 1) {

return elements.get(0);

}

return lastRecursive(elements.subList(1, elements.size()));

}

}

你可能感兴趣的:(java,list最后一个元素)