Here you can find the source of lastIndexOf(final Object[] array, final Object objectToFind)
Find the last index of the given object within the array.
Parameter | Description |
---|---|
array | the array to travers backwords looking for the object, may be <code>null</code> |
objectToFind | the object to find, may be <code>null</code> |
-1
if not found or null
array input
public static int lastIndexOf(final Object[] array, final Object objectToFind)
//package com.java2s; /* ==================================================================== * The Apache Software License, Version 1.1 * * Copyright (c) 2002-2003 The Apache Software Foundation. All rights * reserved.//www . j a v a 2s . co m * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The end-user documentation included with the redistribution, if * any, must include the following acknowledgement: * "This product includes software developed by the * Apache Software Foundation (http://www.apache.org/)." * Alternately, this acknowledgement may appear in the software itself, * if and wherever such third-party acknowledgements normally appear. * * 4. The names "The Jakarta Project", "Commons", and "Apache Software * Foundation" must not be used to endorse or promote products derived * from this software without prior written permission. For written * permission, please contact apache@apache.org. * * 5. Products derived from this software may not be called "Apache" * nor may "Apache" appear in their names without prior written * permission of the Apache Software Foundation. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of the Apache Software Foundation. For more * information on the Apache Software Foundation, please see * <http://www.apache.org/>. */ public class Main { /** * <p> * Find the last index of the given object within the array. * </p> * * <p> * This method returns <code>-1</code> if <code>null</code> array input. * </p> * * @param array * the array to travers backwords looking for the object, may be * <code>null</code> * @param objectToFind * the object to find, may be <code>null</code> * @return the last index of the object within the array, <code>-1</code> * if not found or <code>null</code> array input */ public static int lastIndexOf(final Object[] array, final Object objectToFind) { return lastIndexOf(array, objectToFind, Integer.MAX_VALUE); } /** * <p> * Find the last index of the given object in the array starting at the * given index. * </p> * * <p> * This method returns <code>-1</code> if <code>null</code> array input. * </p> * * <p> * A negative startIndex will return <code>-1</code>. A startIndex larger * than the array length will search from the end of the array. * </p> * * @param array * the array to traverse for looking for the object, may be * <code>null</code> * @param objectToFind * the object to find, may be <code>null</code> * @param startIndex * the start index to travers backwards from * @return the last index of the object within the array, <code>-1</code> * if not found or <code>null</code> array input */ public static int lastIndexOf(final Object[] array, final Object objectToFind, int startIndex) { if (array == null) { return -1; } if (startIndex < 0) { return -1; } else if (startIndex >= array.length) { startIndex = array.length - 1; } if (objectToFind == null) { for (int i = startIndex; i >= 0; i--) { if (array[i] == null) { return i; } } } else { for (int i = startIndex; i >= 0; i--) { if (objectToFind.equals(array[i])) { return i; } } } return -1; } /** * <p> * Find the last index of the given value within the array. * </p> * * <p> * This method returns <code>-1</code> if <code>null</code> array input. * </p> * * @param array * the array to travers backwords looking for the object, may be * <code>null</code> * @param valueToFind * the object to find * @return the last index of the value within the array, <code>-1</code> * if not found or <code>null</code> array input */ public static int lastIndexOf(final long[] array, final long valueToFind) { return lastIndexOf(array, valueToFind, Integer.MAX_VALUE); } /** * <p> * Find the last index of the given value in the array starting at the given * index. * </p> * * <p> * This method returns <code>-1</code> if <code>null</code> array input. * </p> * * <p> * A negative startIndex will return -1. A startIndex larger than the array * length will search from the end of the array. * </p> * * @param array * the array to traverse for looking for the object, may be * <code>null</code> * @param valueToFind * the value to find * @param startIndex * the start index to travers backwards from * @return the last index of the value within the array, <code>-1</code> * if not found or <code>null</code> array input */ public static int lastIndexOf(final long[] array, final long valueToFind, int startIndex) { if (array == null) { return -1; } if (startIndex < 0) { return -1; } else if (startIndex >= array.length) { startIndex = array.length - 1; } for (int i = startIndex; i >= 0; i--) { if (valueToFind == array[i]) { return i; } } return -1; } /** * <p> * Find the last index of the given value within the array. * </p> * * <p> * This method returns <code>-1</code> if <code>null</code> array input. * </p> * * @param array * the array to travers backwords looking for the object, may be * <code>null</code> * @param valueToFind * the object to find * @return the last index of the value within the array, <code>-1</code> * if not found or <code>null</code> array input */ public static int lastIndexOf(final int[] array, final int valueToFind) { return lastIndexOf(array, valueToFind, Integer.MAX_VALUE); } /** * <p> * Find the last index of the given value in the array starting at the given * index. * </p> * * <p> * This method returns <code>-1</code> if <code>null</code> array input. * </p> * * <p> * A negative startIndex will return -1. A startIndex larger than the array * length will search from the end of the array. * </p> * * @param array * the array to traverse for looking for the object, may be * <code>null</code> * @param valueToFind * the value to find * @param startIndex * the start index to travers backwards from * @return the last index of the value within the array, <code>-1</code> * if not found or <code>null</code> array input */ public static int lastIndexOf(final int[] array, final int valueToFind, int startIndex) { if (array == null) { return -1; } if (startIndex < 0) { return -1; } else if (startIndex >= array.length) { startIndex = array.length - 1; } for (int i = startIndex; i >= 0; i--) { if (valueToFind == array[i]) { return i; } } return -1; } /** * <p> * Find the last index of the given value within the array. * </p> * * <p> * This method returns <code>-1</code> if <code>null</code> array input. * </p> * * @param array * the array to travers backwords looking for the object, may be * <code>null</code> * @param valueToFind * the object to find * @return the last index of the value within the array, <code>-1</code> * if not found or <code>null</code> array input */ public static int lastIndexOf(final short[] array, final short valueToFind) { return lastIndexOf(array, valueToFind, Integer.MAX_VALUE); } /** * <p> * Find the last index of the given value in the array starting at the given * index. * </p> * * <p> * This method returns <code>-1</code> if <code>null</code> array input. * </p> * * <p> * A negative startIndex will return -1. A startIndex larger than the array * length will search from the end of the array. * </p> * * @param array * the array to traverse for looking for the object, may be * <code>null</code> * @param valueToFind * the value to find * @param startIndex * the start index to travers backwards from * @return the last index of the value within the array, <code>-1</code> * if not found or <code>null</code> array input */ public static int lastIndexOf(final short[] array, final short valueToFind, int startIndex) { if (array == null) { return -1; } if (startIndex < 0) { return -1; } else if (startIndex >= array.length) { startIndex = array.length - 1; } for (int i = startIndex; i >= 0; i--) { if (valueToFind == array[i]) { return i; } } return -1; } /** * <p> * Find the last index of the given value within the array. * </p> * * <p> * This method returns <code>-1</code> if <code>null</code> array input. * </p> * * @param array * the array to travers backwords looking for the object, may be * <code>null</code> * @param valueToFind * the object to find * @return the last index of the value within the array, <code>-1</code> * if not found or <code>null</code> array input */ public static int lastIndexOf(final byte[] array, final byte valueToFind) { return lastIndexOf(array, valueToFind, Integer.MAX_VALUE); } /** * <p> * Find the last index of the given value in the array starting at the given * index. * </p> * * <p> * This method returns <code>-1</code> if <code>null</code> array input. * </p> * * <p> * A negative startIndex will return -1. A startIndex larger than the array * length will search from the end of the array. * </p> * * @param array * the array to traverse for looking for the object, may be * <code>null</code> * @param valueToFind * the value to find * @param startIndex * the start index to travers backwards from * @return the last index of the value within the array, <code>-1</code> * if not found or <code>null</code> array input */ public static int lastIndexOf(final byte[] array, final byte valueToFind, int startIndex) { if (array == null) { return -1; } if (startIndex < 0) { return -1; } else if (startIndex >= array.length) { startIndex = array.length - 1; } for (int i = startIndex; i >= 0; i--) { if (valueToFind == array[i]) { return i; } } return -1; } /** * <p> * Find the last index of the given value within the array. * </p> * * <p> * This method returns <code>-1</code> if <code>null</code> array input. * </p> * * @param array * the array to travers backwords looking for the object, may be * <code>null</code> * @param valueToFind * the object to find * @return the last index of the value within the array, <code>-1</code> * if not found or <code>null</code> array input */ public static int lastIndexOf(final double[] array, final double valueToFind) { return lastIndexOf(array, valueToFind, Integer.MAX_VALUE); } /** * <p> * Find the last index of the given value within a given tolerance in the * array. This method will return the index of the last value which falls * between the region defined by valueToFind - tolerance and valueToFind + * tolerance. * </p> * * <p> * This method returns <code>-1</code> if <code>null</code> array input. * </p> * * @param array * the array to search through for the object, may be * <code>null</code> * @param valueToFind * the value to find * @param tolerance * tolerance of the search * @return the index of the value within the array, <code>-1</code> if not * found or <code>null</code> array input */ public static int lastIndexOf(final double[] array, final double valueToFind, final double tolerance) { return lastIndexOf(array, valueToFind, Integer.MAX_VALUE, tolerance); } /** * <p> * Find the last index of the given value in the array starting at the given * index. * </p> * * <p> * This method returns <code>-1</code> if <code>null</code> array input. * </p> * * <p> * A negative startIndex will return -1. A startIndex larger than the array * length will search from the end of the array. * </p> * * @param array * the array to traverse for looking for the object, may be * <code>null</code> * @param valueToFind * the value to find * @param startIndex * the start index to travers backwards from * @return the last index of the value within the array, <code>-1</code> * if not found or <code>null</code> array input */ public static int lastIndexOf(final double[] array, final double valueToFind, int startIndex) { if (array == null || array.length == 0) { return -1; } if (startIndex < 0) { return -1; } else if (startIndex >= array.length) { startIndex = array.length - 1; } for (int i = startIndex; i >= 0; i--) { if (valueToFind == array[i]) { return i; } } return -1; } /** * <p> * Find the last index of the given value in the array starting at the given * index. This method will return the index of the last value which falls * between the region defined by valueToFind - tolerance and valueToFind + * tolerance. * </p> * * <p> * This method returns <code>-1</code> if <code>null</code> array input. * </p> * * <p> * A negative startIndex will return -1. A startIndex larger than the array * length will search from the end of the array. * </p> * * @param array * the array to traverse for looking for the object, may be * <code>null</code> * @param valueToFind * the value to find * @param startIndex * the start index to travers backwards from * @param tolerance * search for value within plus/minus this amount * @return the last index of the value within the array, <code>-1</code> * if not found or <code>null</code> array input */ public static int lastIndexOf(final double[] array, final double valueToFind, int startIndex, double tolerance) { if (array == null || array.length == 0) { return -1; } if (startIndex < 0) { return -1; } else if (startIndex >= array.length) { startIndex = array.length - 1; } double min = valueToFind - tolerance; double max = valueToFind + tolerance; for (int i = startIndex; i >= 0; i--) { if (array[i] >= min && array[i] <= max) { return i; } } return -1; } /** * <p> * Find the last index of the given value within the array. * </p> * * <p> * This method returns <code>-1</code> if <code>null</code> array input. * </p> * * @param array * the array to travers backwords looking for the object, may be * <code>null</code> * @param valueToFind * the object to find * @return the last index of the value within the array, <code>-1</code> * if not found or <code>null</code> array input */ public static int lastIndexOf(final float[] array, final float valueToFind) { return lastIndexOf(array, valueToFind, Integer.MAX_VALUE); } /** * <p> * Find the last index of the given value in the array starting at the given * index. * </p> * * <p> * This method returns <code>-1</code> if <code>null</code> array input. * </p> * * <p> * A negative startIndex will return -1. A startIndex larger than the array * length will search from the end of the array. * </p> * * @param array * the array to traverse for looking for the object, may be * <code>null</code> * @param valueToFind * the value to find * @param startIndex * the start index to travers backwards from * @return the last index of the value within the array, <code>-1</code> * if not found or <code>null</code> array input */ public static int lastIndexOf(final float[] array, final float valueToFind, int startIndex) { if (array == null || array.length == 0) { return -1; } if (startIndex < 0) { return -1; } else if (startIndex >= array.length) { startIndex = array.length - 1; } for (int i = startIndex; i >= 0; i--) { if (valueToFind == array[i]) { return i; } } return -1; } /** * <p> * Find the last index of the given value within the array. * </p> * * <p> * This method returns <code>-1</code> if <code>null</code> array input. * </p> * * @param array * the array to travers backwords looking for the object, may be * <code>null</code> * @param valueToFind * the object to find * @return the last index of the value within the array, <code>-1</code> * if not found or <code>null</code> array input */ public static int lastIndexOf(final boolean[] array, final boolean valueToFind) { return lastIndexOf(array, valueToFind, Integer.MAX_VALUE); } /** * <p> * Find the last index of the given value in the array starting at the given * index. * </p> * * <p> * This method returns <code>-1</code> if <code>null</code> array input. * </p> * * <p> * A negative startIndex will return -1. A startIndex larger than the array * length will search from the end of the array. * </p> * * @param array * the array to traverse for looking for the object, may be * <code>null</code> * @param valueToFind * the value to find * @param startIndex * the start index to travers backwards from * @return the last index of the value within the array, <code>-1</code> * if not found or <code>null</code> array input */ public static int lastIndexOf(final boolean[] array, final boolean valueToFind, int startIndex) { if (array == null || array.length == 0) { return -1; } if (startIndex < 0) { return -1; } else if (startIndex >= array.length) { startIndex = array.length - 1; } for (int i = startIndex; i >= 0; i--) { if (valueToFind == array[i]) { return i; } } return -1; } }