Returns the last element of an array list. - Java java.util

Java examples for java.util:List First Element

Description

Returns the last element of an array list.

Demo Code


//package com.java2s;

import java.util.ArrayList;

public class Main {
    /**//from  w  ww .j  a  v a  2 s  . c  o  m
     * Returns the last element of an array list.
     * Returns null if list is empty or null
     * 
     * @param list the ArrayList to search through
     * @return List the resulting element
     */

    public static Object getLastElementOfArrayList(ArrayList list) {
        if (list != null || list.size() >= 0) {
            return list.get(list.size() - 1);
        }
        return null;
    }
}

Related Tutorials