Java Array Last Index Of lastIndexOf(final byte[] str, int startIndex, int endIndex, final byte ch)

Here you can find the source of lastIndexOf(final byte[] str, int startIndex, int endIndex, final byte ch)

Description

last Index Of

License

Apache License

Declaration

public static int lastIndexOf(final byte[] str, int startIndex, int endIndex, final byte ch) 

Method Source Code

//package com.java2s;
/*/*from   w ww .java  2s  .c o  m*/
 * Copyright (c) 2014  Haixing Hu
 *
 * 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.
 *
 */

public class Main {
    public static int lastIndexOf(final byte[] str, int startIndex, int endIndex, final byte ch) {
        if (startIndex < 0) {
            startIndex = 0;
        }
        if (endIndex > str.length) {
            endIndex = str.length;
        }
        for (int i = endIndex - 1; i >= startIndex; --i) {
            if (str[i] == ch) {
                return i;
            }
        }
        return -1;
    }

    public static int lastIndexOf(final byte[] str1, int startIndex1, int endIndex1, final byte[] str2,
            int startIndex2, int endIndex2) {
        if (startIndex1 < 0) {
            startIndex1 = 0;
        }
        if (endIndex1 > str1.length) {
            endIndex1 = str1.length;
        }
        if (startIndex2 < 0) {
            startIndex2 = 0;
        }
        if (endIndex2 > str2.length) {
            endIndex2 = str2.length;
        }
        final int len1 = endIndex1 - startIndex1;
        if (len1 <= 0) {
            return -1;
        }
        final int len2 = endIndex2 - startIndex2;
        if (len2 <= 0) {
            return endIndex1 - 1;
        }
        if (len1 < len2) {
            return -1;
        }
        final byte last2 = str2[endIndex2 - 1];
        final int newStartIndex1 = startIndex1 + (len2 - 1);
        for (int i = endIndex1 - 1; i >= newStartIndex1; --i) {
            if (str1[i] == last2) {
                boolean match = true;
                for (int j = i - 1, k = endIndex2 - 2; k >= startIndex2; --j, --k) {
                    assert (j >= startIndex1);
                    if (str1[j] != str2[k]) {
                        match = false;
                        break;
                    }
                }
                if (match) {
                    return i;
                }
            }
        }
        return -1;
    }
}

Related

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