Java Array Last Index Of lastIndexOf(byte[] ary, byte value)

Here you can find the source of lastIndexOf(byte[] ary, byte value)

Description

Search for the last matching byte in an array of bytes

License

Open Source License

Parameter

Parameter Description
ary the array of values to search
value the value to search for

Return

the index of the matching value, or -1 if the value could not be found

Declaration

public static final int lastIndexOf(byte[] ary, byte value) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    /** Search for the last matching boolean in an array of booleans
     * @param ary the array of values to search
     * @param value the value to search for
     * @return the index of the matching value, or -1 if the {@code value} could not be found
     *//*from   ww  w  . java  2 s.co  m*/
    public static final int lastIndexOf(boolean[] ary, boolean value) {
        return lastIndexOf(ary, 0, ary.length, value);
    }

    /** Search for the last matching boolean in an array of booleans
     * @param ary the array of values to search
     * @param off the offset into {@code ary} at which to stop searching for values
     * @param len the number of values to search for starting at {@code off + len} in {@code ary}
     * @param value the value to search for
     * @return the index of the matching value, or -1 if the {@code value} could not be found
     */
    public static final int lastIndexOf(boolean[] ary, int off, int len, boolean value) {
        for (int i = off + len - 1; i >= off; i--) {
            if (ary[i] == (value)) {
                return i;
            }
        }
        return -1;
    }

    /** Search for the last matching byte in an array of bytes
     * @param ary the array of values to search
     * @param value the value to search for
     * @return the index of the matching value, or -1 if the {@code value} could not be found
     */
    public static final int lastIndexOf(byte[] ary, byte value) {
        return lastIndexOf(ary, 0, ary.length, value);
    }

    /** Search for the last matching byte in an array of bytes
     * @param ary the array of values to search
     * @param off the offset into {@code ary} at which to stop searching for values
     * @param len the number of values to search for starting at {@code off + len} in {@code ary}
     * @param value the value to search for
     * @return the index of the matching value, or -1 if the {@code value} could not be found
     */
    public static final int lastIndexOf(byte[] ary, int off, int len, byte value) {
        for (int i = off + len - 1; i >= off; i--) {
            if (ary[i] == (value)) {
                return i;
            }
        }
        return -1;
    }

    /** Search for the last matching short in an array of shorts
     * @param ary the array of values to search
     * @param value the value to search for
     * @return the index of the matching value, or -1 if the {@code value} could not be found
     */
    public static final int lastIndexOf(short[] ary, short value) {
        return lastIndexOf(ary, 0, ary.length, value);
    }

    /** Search for the last matching short in an array of shorts
     * @param ary the array of values to search
     * @param off the offset into {@code ary} at which to stop searching for values
     * @param len the number of values to search for starting at {@code off + len} in {@code ary}
     * @param value the value to search for
     * @return the index of the matching value, or -1 if the {@code value} could not be found
     */
    public static final int lastIndexOf(short[] ary, int off, int len, short value) {
        for (int i = off + len - 1; i >= off; i--) {
            if (ary[i] == (value)) {
                return i;
            }
        }
        return -1;
    }

    /** Search for the last matching char in an array of chars
     * @param ary the array of values to search
     * @param value the value to search for
     * @return the index of the matching value, or -1 if the {@code value} could not be found
     */
    public static final int lastIndexOf(char[] ary, char value) {
        return lastIndexOf(ary, 0, ary.length, value);
    }

    /** Search for the last matching char in an array of chars
     * @param ary the array of values to search
     * @param off the offset into {@code ary} at which to stop searching for values
     * @param len the number of values to search for starting at {@code off + len} in {@code ary}
     * @param value the value to search for
     * @return the index of the matching value, or -1 if the {@code value} could not be found
     */
    public static final int lastIndexOf(char[] ary, int off, int len, char value) {
        for (int i = off + len - 1; i >= off; i--) {
            if (ary[i] == (value)) {
                return i;
            }
        }
        return -1;
    }

    /** Search for the last matching int in an array of ints
     * @param ary the array of values to search
     * @param value the value to search for
     * @return the index of the matching value, or -1 if the {@code value} could not be found
     */
    public static final int lastIndexOf(int[] ary, int value) {
        return lastIndexOf(ary, 0, ary.length, value);
    }

    /** Search for the last matching int in an array of ints
     * @param ary the array of values to search
     * @param off the offset into {@code ary} at which to stop searching for values
     * @param len the number of values to search for starting at {@code off + len} in {@code ary}
     * @param value the value to search for
     * @return the index of the matching value, or -1 if the {@code value} could not be found
     */
    public static final int lastIndexOf(int[] ary, int off, int len, int value) {
        for (int i = off + len - 1; i >= off; i--) {
            if (ary[i] == (value)) {
                return i;
            }
        }
        return -1;
    }

    /** Search for the last matching long in an array of longs
     * @param ary the array of values to search
     * @param value the value to search for
     * @return the index of the matching value, or -1 if the {@code value} could not be found
     */
    public static final int lastIndexOf(long[] ary, long value) {
        return lastIndexOf(ary, 0, ary.length, value);
    }

    /** Search for the last matching long in an array of longs
     * @param ary the array of values to search
     * @param off the offset into {@code ary} at which to stop searching for values
     * @param len the number of values to search for starting at {@code off + len} in {@code ary}
     * @param value the value to search for
     * @return the index of the matching value, or -1 if the {@code value} could not be found
     */
    public static final int lastIndexOf(long[] ary, int off, int len, long value) {
        for (int i = off + len - 1; i >= off; i--) {
            if (ary[i] == (value)) {
                return i;
            }
        }
        return -1;
    }

    /** Search for the last matching float in an array of floats
     * @param ary the array of values to search
     * @param value the value to search for
     * @return the index of the matching value, or -1 if the {@code value} could not be found
     */
    public static final int lastIndexOf(float[] ary, float value) {
        return lastIndexOf(ary, 0, ary.length, value);
    }

    /** Search for the last matching float in an array of floats
     * @param ary the array of values to search
     * @param off the offset into {@code ary} at which to stop searching for values
     * @param len the number of values to search for starting at {@code off + len} in {@code ary}
     * @param value the value to search for
     * @return the index of the matching value, or -1 if the {@code value} could not be found
     */
    public static final int lastIndexOf(float[] ary, int off, int len, float value) {
        for (int i = off + len - 1; i >= off; i--) {
            if (ary[i] == (value)) {
                return i;
            }
        }
        return -1;
    }

    /** Search for the last matching double in an array of doubles
     * @param ary the array of values to search
     * @param value the value to search for
     * @return the index of the matching value, or -1 if the {@code value} could not be found
     */
    public static final int lastIndexOf(double[] ary, double value) {
        return lastIndexOf(ary, 0, ary.length, value);
    }

    /** Search for the last matching double in an array of doubles
     * @param ary the array of values to search
     * @param off the offset into {@code ary} at which to stop searching for values
     * @param len the number of values to search for starting at {@code off + len} in {@code ary}
     * @param value the value to search for
     * @return the index of the matching value, or -1 if the {@code value} could not be found
     */
    public static final int lastIndexOf(double[] ary, int off, int len, double value) {
        for (int i = off + len - 1; i >= off; i--) {
            if (ary[i] == (value)) {
                return i;
            }
        }
        return -1;
    }

    /** Search for the last matching T in an array of Ts
     * @param ary the array of values to search
     * @param value the value to search for
     * @return the index of the matching value, or -1 if the {@code value} could not be found
     */
    public static final <T> int lastIndexOf(T[] ary, T value) {
        return lastIndexOf(ary, 0, ary.length, value);
    }

    /** Search for the last matching T in an array of Ts
     * @param ary the array of values to search
     * @param off the offset into {@code ary} at which to stop searching for values
     * @param len the number of values to search for starting at {@code off + len} in {@code ary}
     * @param value the value to search for
     * @return the index of the matching value, or -1 if the {@code value} could not be found
     */
    public static final <T> int lastIndexOf(T[] ary, int off, int len, T value) {
        for (int i = off + len - 1; i >= off; i--) {
            if (ary[i].equals(value)) {
                return i;
            }
        }
        return -1;
    }

    /** Compare two sub-arrays for equality to one another
     * @param a the first array to compare
     * @param offA the offset into {@code a} at which to start comparing
     * @param b the second array to compare
     * @param offB the offset into {@code b} at which to start comparing
     * @param len the number of elements from {@code a} to compare to elements from {@code b}
     * @return true if {@code len} number of elements from {@code a} and {@code b} are equal, false
     * if one or more elements are not equal
     */
    public static final boolean equals(boolean[] a, int offA, boolean[] b, int offB, int len) {
        if (a == b) {
            return true;
        }
        if (a == null || b == null) {
            return false;
        }

        for (int i = 0; i < len; i++) {
            boolean objA = a[i];
            boolean objB = b[i];
            if (objA != objB) {
                return false;
            }
        }
        return true;
    }

    /** Compare two sub-arrays for equality to one another
     * @param a the first array to compare
     * @param offA the offset into {@code a} at which to start comparing
     * @param b the second array to compare
     * @param offB the offset into {@code b} at which to start comparing
     * @param len the number of elements from {@code a} to compare to elements from {@code b}
     * @return true if {@code len} number of elements from {@code a} and {@code b} are equal, false
     * if one or more elements are not equal
     */
    public static final boolean equals(byte[] a, int offA, byte[] b, int offB, int len) {
        if (a == b) {
            return true;
        }
        if (a == null || b == null) {
            return false;
        }

        for (int i = 0; i < len; i++) {
            byte objA = a[i];
            byte objB = b[i];
            if (objA != objB) {
                return false;
            }
        }
        return true;
    }

    /** Compare two sub-arrays for equality to one another
     * @param a the first array to compare
     * @param offA the offset into {@code a} at which to start comparing
     * @param b the second array to compare
     * @param offB the offset into {@code b} at which to start comparing
     * @param len the number of elements from {@code a} to compare to elements from {@code b}
     * @return true if {@code len} number of elements from {@code a} and {@code b} are equal, false
     * if one or more elements are not equal
     */
    public static final boolean equals(short[] a, int offA, short[] b, int offB, int len) {
        if (a == b) {
            return true;
        }
        if (a == null || b == null) {
            return false;
        }

        for (int i = 0; i < len; i++) {
            short objA = a[i];
            short objB = b[i];
            if (objA != objB) {
                return false;
            }
        }
        return true;
    }

    /** Compare two sub-arrays for equality to one another
     * @param a the first array to compare
     * @param offA the offset into {@code a} at which to start comparing
     * @param b the second array to compare
     * @param offB the offset into {@code b} at which to start comparing
     * @param len the number of elements from {@code a} to compare to elements from {@code b}
     * @return true if {@code len} number of elements from {@code a} and {@code b} are equal, false
     * if one or more elements are not equal
     */
    public static final boolean equals(char[] a, int offA, char[] b, int offB, int len) {
        if (a == b) {
            return true;
        }
        if (a == null || b == null) {
            return false;
        }

        for (int i = 0; i < len; i++) {
            char objA = a[i];
            char objB = b[i];
            if (objA != objB) {
                return false;
            }
        }
        return true;
    }

    /** Compare two sub-arrays for equality to one another
     * @param a the first array to compare
     * @param offA the offset into {@code a} at which to start comparing
     * @param b the second array to compare
     * @param offB the offset into {@code b} at which to start comparing
     * @param len the number of elements from {@code a} to compare to elements from {@code b}
     * @return true if {@code len} number of elements from {@code a} and {@code b} are equal, false
     * if one or more elements are not equal
     */
    public static final boolean equals(int[] a, int offA, int[] b, int offB, int len) {
        if (a == b) {
            return true;
        }
        if (a == null || b == null) {
            return false;
        }

        for (int i = 0; i < len; i++) {
            int objA = a[i];
            int objB = b[i];
            if (objA != objB) {
                return false;
            }
        }
        return true;
    }

    /** Compare two sub-arrays for equality to one another
     * @param a the first array to compare
     * @param offA the offset into {@code a} at which to start comparing
     * @param b the second array to compare
     * @param offB the offset into {@code b} at which to start comparing
     * @param len the number of elements from {@code a} to compare to elements from {@code b}
     * @return true if {@code len} number of elements from {@code a} and {@code b} are equal, false
     * if one or more elements are not equal
     */
    public static final boolean equals(long[] a, int offA, long[] b, int offB, int len) {
        if (a == b) {
            return true;
        }
        if (a == null || b == null) {
            return false;
        }

        for (int i = 0; i < len; i++) {
            long objA = a[i];
            long objB = b[i];
            if (objA != objB) {
                return false;
            }
        }
        return true;
    }

    /** Compare two sub-arrays for equality to one another
     * @param a the first array to compare
     * @param offA the offset into {@code a} at which to start comparing
     * @param b the second array to compare
     * @param offB the offset into {@code b} at which to start comparing
     * @param len the number of elements from {@code a} to compare to elements from {@code b}
     * @return true if {@code len} number of elements from {@code a} and {@code b} are equal, false
     * if one or more elements are not equal
     */
    public static final boolean equals(float[] a, int offA, float[] b, int offB, int len) {
        if (a == b) {
            return true;
        }
        if (a == null || b == null) {
            return false;
        }

        for (int i = 0; i < len; i++) {
            float objA = a[i];
            float objB = b[i];
            if (objA != objB) {
                return false;
            }
        }
        return true;
    }

    /** Compare two sub-arrays for equality to one another
     * @param a the first array to compare
     * @param offA the offset into {@code a} at which to start comparing
     * @param b the second array to compare
     * @param offB the offset into {@code b} at which to start comparing
     * @param len the number of elements from {@code a} to compare to elements from {@code b}
     * @return true if {@code len} number of elements from {@code a} and {@code b} are equal, false
     * if one or more elements are not equal
     */
    public static final boolean equals(double[] a, int offA, double[] b, int offB, int len) {
        if (a == b) {
            return true;
        }
        if (a == null || b == null) {
            return false;
        }

        for (int i = 0; i < len; i++) {
            double objA = a[i];
            double objB = b[i];
            if (objA != objB) {
                return false;
            }
        }
        return true;
    }

    /** Compare two sub-arrays for equality to one another
     * @param a the first array to compare
     * @param offA the offset into {@code a} at which to start comparing
     * @param b the second array to compare
     * @param offB the offset into {@code b} at which to start comparing
     * @param len the number of elements from {@code a} to compare to elements from {@code b}
     * @return true if {@code len} number of elements from {@code a} and {@code b} are equal, false
     * if one or more elements are not equal
     */
    public static final <T> boolean equals(T[] a, int offA, T[] b, int offB, int len) {
        if (a == b) {
            return true;
        }
        if (a == null || b == null) {
            return false;
        }

        for (int i = 0; i < len; i++) {
            T objA = a[i];
            T objB = b[i];
            if (!(objA == null ? objB == null : objA.equals(objB))) {
                return false;
            }
        }
        return true;
    }
}

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[] array, byte valueToFind)
  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)
  9. lastIndexOf(byte[] s, char c)