Java Array Element Get get(T[] array, int index)

Here you can find the source of get(T[] array, int index)

Description

Returns the value at the given index in the arrayor null if the index is out of bounds.

License

LGPL

Parameter

Parameter Description
array the array
index the index

Return

the element at index index or null

Declaration

public static <T> T get(T[] array, int index) 

Method Source Code

//package com.java2s;
//License from project: LGPL 

import java.util.*;

public class Main {
    /**//from  w  w w.j  a  v a2 s . c o  m
     * <p>Returns the value at the given index in the arrayor null if the index is out of bounds.</p>
     * @param array the array
     * @param index the index
     * @return the element at index {@code index} or null
     */
    public static <T> T get(T[] array, int index) {
        return index >= 0 && index < array.length ? array[index] : null;
    }

    /**
     * <p>Returns the value at the given index in the array or {@code defaultValue} if the index is out of bounds.</p>
     * @param array the array
     * @param index the index
     * @param defaultValue the default value
     * @return the element at index {@code index} or the default value
     */
    public static <T, D extends T, R extends T> T get(R[] array, int index, D defaultValue) {
        return index >= 0 && index < array.length ? array[index] : defaultValue;
    }

    /**
     * <p>Returns the value at the given index in the List or null if the index is out of bounds.</p>
     * @param list the List
     * @param index the index
     * @return the element at index {@code index} or null
     */
    public static <T> T get(List<T> list, int index) {
        return index >= 0 && index < list.size() ? list.get(index) : null;
    }

    /**
     * <p>Returns the value at the given index in the List or {@code defaultValue} if the index is out of bounds.</p>
     * @param list the List
     * @param index the index
     * @param defaultValue the default value
     * @return the element at index {@code index} or the default value
     */
    public static <T, D extends T, V extends T> T get(List<V> list, int index, D defaultValue) {
        return index >= 0 && index < list.size() ? list.get(index) : defaultValue;
    }
}

Related

  1. arrayElement(T[] array, int index)
  2. get(int index, final T[]... arrays)
  3. get(Object[] array, int index)
  4. get(T[] array, int index)
  5. get(T[] array, int index)
  6. getAllMatches(String[] target, String[] pattern)
  7. getArgPairsSeparatedByChar(String[] args, String separator)
  8. getArgs(final String[] args, final String[] defaultValue, final String... flags)