Java Array Last Index Of lastIndexOf(final byte[] reference, final byte[] query)

Here you can find the source of lastIndexOf(final byte[] reference, final byte[] query)

Description

Find the last occurrence of the query sequence in the reference sequence Returns the index of the last occurrence or -1 if the query sequence is not found

License

Open Source License

Parameter

Parameter Description
reference the reference sequence
query the query sequence

Declaration

public static int lastIndexOf(final byte[] reference, final byte[] query) 

Method Source Code

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

public class Main {
    /**//w  w  w .ja  v  a2s .  c  om
     * Find the last occurrence of the query sequence in the reference sequence
     *
     * Returns the index of the last occurrence or -1 if the query sequence is not found
     *
     * @param reference the reference sequence
     * @param query the query sequence
     */
    public static int lastIndexOf(final byte[] reference, final byte[] query) {
        int queryLength = query.length;

        // start search from the last possible matching position and search to the left
        for (int r = reference.length - queryLength; r >= 0; r--) {
            int q = 0;
            while (q < queryLength && reference[r + q] == query[q]) {
                q++;
            }
            if (q == queryLength) {
                return r;
            }
        }
        return -1;
    }
}

Related

  1. lastIndexOf(byte[] pattern, byte[] block)
  2. lastIndexOf(byte[] s, char c)
  3. lastIndexOf(byte[] source, byte[] match)
  4. lastIndexOf(char[] toBeFound, char[] array)
  5. lastIndexOf(char[] toBeFound, char[] array)
  6. lastIndexOf(final byte[] str, int startIndex, int endIndex, final byte ch)
  7. lastIndexOf(final char[] source, final int start, final int end, final char ch)
  8. lastIndexOf(final Object[] array, final Object objectToFind)
  9. lastIndexOf(int[] array, int intToFind)