Gets the element in the given list with the given index, or null if the list is null or if the index is outside the range or if the value at the given index is null. - Java java.util

Java examples for java.util:List Element

Description

Gets the element in the given list with the given index, or null if the list is null or if the index is outside the range or if the value at the given index is null.

Demo Code


//package com.book2s;

import java.util.List;

public class Main {
    public static void main(String[] argv) {
        List c = java.util.Arrays.asList("asdf", "book2s.com");
        int index = 42;
        System.out.println(getOrNull(c, index));
    }/*from www.j  ava2  s.  c  o m*/

    /**
     * Gets the element in the given list with the given index,
     * or null if the list is null or if the index is outside the range
     * or if the value at the given index is null.
     */
    public static <T> T getOrNull(List<T> c, int index) {
        if (c == null)
            return null;
        if (index < 0 || index >= c.size())
            return null;
        return c.get(index);
    }
}

Related Tutorials