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

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

Description

Gets the element at an index in the array, but returns null if the index is outside of the array bounds.

License

Open Source License

Parameter

Parameter Description
T the array type.
array the array to use.
index the index to use.

Return

array[index] or null if out of bounds.

Declaration

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

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2009-2016 Black Rook Software
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser Public License v2.1
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html
 ******************************************************************************/

public class Main {
    /**//from  w  w  w.j  av a 2  s .c o  m
     * Gets the element at an index in the array, but returns 
     * null if the index is outside of the array bounds.
     * @param <T> the array type.
     * @param array the array to use.
     * @param index the index to use.
     * @return <code>array[index]</code> or null if out of bounds.
     * @since 2.31.2
     */
    public static <T> T arrayElement(T[] array, int index) {
        if (index < 0 || index >= array.length)
            return null;
        else
            return array[index];
    }
}

Related

  1. get(int index, final T[]... arrays)
  2. get(Object[] array, int index)
  3. get(T[] array, int index)
  4. get(T[] array, int index)