get Nth Element in List - Java java.util

Java examples for java.util:List Element

Description

get Nth Element in List

Demo Code


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

import java.util.List;

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

    static <T extends Comparable<T>> T getNthElement(int n, List<T> list) {
        List<T> newList = new ArrayList<T>(list);
        Collections.sort(newList);
        return newList.get(n);
    }
}

Related Tutorials