take Last number of element from List - Android java.util

Android examples for java.util:List Element

Description

take Last number of element from List

Demo Code


//package com.book2s;
import java.util.ArrayList;
import java.util.List;

public class Main {
    public static void main(String[] argv) {
        List list = java.util.Arrays.asList("asdf", "book2s.com");
        int length = 42;
        System.out.println(takeLast(list, length));
    }/*from   w w w  .  j  a  va 2 s.c  o m*/

    public static <T> List<T> takeLast(List<T> list, int length) {
        if (list.size() > length) {
            int fromIndex = Math.max(0, list.size() - length);
            int toIndex = list.size();
            List<T> newList = new ArrayList<T>();
            newList.addAll(list.subList(fromIndex, toIndex));
            return newList;
        }
        return list;
    }
}

Related Tutorials