Java Array Last Index Of lastIndexOf(char[] toBeFound, char[] array)

Here you can find the source of lastIndexOf(char[] toBeFound, char[] array)

Description

last Index Of

License

Open Source License

Declaration

public static final int lastIndexOf(char[] toBeFound, char[] array) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2004, 2016 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors://from w w w  . jav  a 2 s .  c  o  m
 *     IBM Corporation - initial API and implementation
 *     Andrew Ferguson (Symbian)
 *     Markus Schorn (Wind River Systems)
 *     Sergey Prigogin (Google)
 *******************************************************************************/

public class Main {
    public static final int lastIndexOf(char[] toBeFound, char[] array) {
        return lastIndexOf(toBeFound, array, 0);
    }

    /**
     * @since 5.11
     */
    public static int lastIndexOf(char toBeFound, char[] array) {
        return lastIndexOf(toBeFound, array, 0);
    }

    /**
     * @since 5.11
     */
    public static int lastIndexOf(char toBeFound, char[] array, int fromIndex) {
        for (int i = array.length; --i >= fromIndex;) {
            if (array[i] == toBeFound) {
                return i;
            }
        }
        return -1;
    }

    /**
     * @since 5.11
     */
    public static int lastIndexOf(char[] toBeFound, char[] array, int fromIndex) {
        int i = array.length;
        int j = toBeFound.length;
        while (true) {
            if (--j < 0)
                return i;
            if (--i < fromIndex)
                return -1;
            if (toBeFound[j] != array[i]) {
                i += toBeFound.length - j - 1;
                j = toBeFound.length;
            }
        }
    }
}

Related

  1. lastIndexOf(byte[] bytes, int pos, int len, byte b)
  2. lastIndexOf(byte[] data, int offset, int length, byte val)
  3. lastIndexOf(byte[] pattern, byte[] block)
  4. lastIndexOf(byte[] s, char c)
  5. lastIndexOf(byte[] source, byte[] match)
  6. lastIndexOf(char[] toBeFound, char[] array)
  7. lastIndexOf(final byte[] reference, final byte[] query)
  8. lastIndexOf(final byte[] str, int startIndex, int endIndex, final byte ch)
  9. lastIndexOf(final char[] source, final int start, final int end, final char ch)