Android examples for java.lang:Array Element
Returns the last index of the given array or -1 if empty or null.
/*//from ww w . j a v a 2 s .c o m * @(#)$Id: ArrayUtils.java 3619 2008-03-26 07:23:03Z yui $ * * Copyright 2006-2008 Makoto YUI * * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except * in compliance with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software distributed under the License * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express * or implied. See the License for the specific language governing permissions and limitations under * the License. * * Contributors: Makoto YUI - ported from jakarta commons lang */ import java.lang.reflect.Array; import java.util.Collection; import java.util.Random; public class Main{ /** * Returns the last index of the given array or -1 if empty or null. This method can deal with * <code>Object</code> arrays and with primitive arrays. This value is one less than the size * since arrays indices are 0-based.</p> * * <pre> * ArrayUtils.lastIndex(null) = -1 * ArrayUtils.lastIndex([]) = -1 * ArrayUtils.lastIndex([null]) = 0 * ArrayUtils.lastIndex([true, false]) = 1 * ArrayUtils.lastIndex([1, 2, 3]) = 2 * ArrayUtils.lastIndex(["a", "b", "c"]) = 2 * </pre> * * @param array * the array to return the last index for, may be null * @return the last index, -1 if empty or null * @throws IllegalArgumentException * if the object arguement is not an array. */ public static int lastIndex(final Object array) { return ArrayUtils.getLength(array) - 1; } /** * <p> * Returns the length of the specified array. This method can deal with <code>Object</code> arrays * and with primitive arrays. * </p> * * <p> * If the input array is <code>null</code>, <code>0</code> is returned. * </p> * * <pre> * ArrayUtils.getLength(null) = 0 * ArrayUtils.getLength([]) = 0 * ArrayUtils.getLength([null]) = 1 * ArrayUtils.getLength([true, false]) = 2 * ArrayUtils.getLength([1, 2, 3]) = 3 * ArrayUtils.getLength(["a", "b", "c"]) = 3 * </pre> * * @param array * the array to retrieve the length from, may be null * @return The length of the array, or <code>0</code> if the array is <code>null</code> * @throws IllegalArgumentException * if the object arguement is not an array. */ public static int getLength(final Object array) { if (array == null) { return 0; } return Array.getLength(array); } }