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

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

Description

get

License

LGPL

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 {
    public static <T> T get(T[] array, int index) {
        if (array.length < index + 1) {
            return null;
        }/*from  w  w w  . j  a  v a2 s.  c o m*/
        return array[index];
    }

    public static <T> T get(final T[] array, final int index, final T defaultValue) {
        if (array.length < index + 1) {
            return defaultValue;
        }
        final T result = array[index];
        return null != result ? result : defaultValue; // array[index];
    }

    public static <T> T get(final Collection<T> collection, final int index) {
        if (collection.size() < index + 1) {
            return null;
        }
        int i = 0;
        for (T item : collection) {
            if (i == index) {
                return item;
            }
            i++;
        }
        return null;
    }

    public static int size(final Object[] array) {
        return null != array ? array.length : 0;
    }

    public static int size(final Collection collection) {
        return null != collection ? collection.size() : 0;
    }

    public static int size(final Map map) {
        return null != map ? map.size() : 0;
    }
}

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)