Java Array Last Index Of lastIndexOf(byte[] array, byte valueToFind)

Here you can find the source of lastIndexOf(byte[] array, byte valueToFind)

Description

last Index Of

License

Apache License

Declaration

public static int lastIndexOf(byte[] array, byte valueToFind) 

Method Source Code

//package com.java2s;
/*//from  w ww.j a  v  a2s  .c o m
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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.
 */

public class Main {
    /**
     * The index value when an element is not found in a list or array: <code>-1</code>. This value is returned by methods in this class and can also be used in comparisons with values returned by various method from {@link java.util.List}.
     */
    public final static int INDEX_NOT_FOUND = -1;

    public static int lastIndexOf(byte[] array, byte valueToFind) {
        return lastIndexOf(array, valueToFind, Integer.MAX_VALUE);
    }

    public static int lastIndexOf(byte[] array, byte valueToFind, int startIndex) {
        if (array == null) {
            return INDEX_NOT_FOUND;
        }
        if (startIndex < 0) {
            return INDEX_NOT_FOUND;
        } else if (startIndex >= array.length) {
            startIndex = array.length - 1;
        }
        for (int i = startIndex; i >= 0; i--) {
            if (valueToFind == array[i]) {
                return i;
            }
        }
        return INDEX_NOT_FOUND;
    }
}

Related

  1. arrayLastIndexOf(final T[] array, final T value)
  2. arrayLastIndexOf(Object[] array, Object objectToFind)
  3. lastIndexOf(boolean[] array, boolean valueToFind)
  4. lastIndexOf(byte value, byte[] array)
  5. lastIndexOf(byte[] ary, byte value)
  6. lastIndexOf(byte[] bytes, int pos, int len, byte b)
  7. lastIndexOf(byte[] data, int offset, int length, byte val)
  8. lastIndexOf(byte[] pattern, byte[] block)