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

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

Description

index Of

License

Open Source License

Declaration

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

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2004, 2015 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:/* ww  w . ja va  2s  .  c om*/
 *     IBM Corporation - initial API and implementation
 *     Andrew Ferguson (Symbian)
 *     Markus Schorn (Wind River Systems)
 *     Sergey Prigogin (Google)
 *******************************************************************************/

import java.util.Arrays;

public class Main {
    public static final int indexOf(char toBeFound, char[] array) {
        for (int i = 0; i < array.length; i++) {
            if (toBeFound == array[i])
                return i;
        }
        return -1;
    }

    public static int indexOf(char toBeFound, char[] buffer, int start,
            int end) {
        if (start < 0 || start > buffer.length || end > buffer.length)
            return -1;

        for (int i = start; i < end; i++) {
            if (toBeFound == buffer[i])
                return i;
        }
        return -1;
    }

    public static final int indexOf(char[] toBeFound, char[] array) {
        if (toBeFound.length > array.length)
            return -1;

        int j = 0;
        for (int i = 0; i < array.length; i++) {
            if (toBeFound[j] == array[i]) {
                if (++j == toBeFound.length)
                    return i - j + 1;
            } else {
                j = 0;
            }
        }
        return -1;
    }

    /**
     * Finds an array of chars in an array of arrays of chars.
     *
     * @return offset where the array was found or {@code -1}
     */
    public static int indexOf(final char[] searchFor,
            final char[][] searchIn) {
        for (int i = 0; i < searchIn.length; i++) {
            if (equals(searchIn[i], searchFor)) {
                return i;
            }
        }
        return -1;
    }

    public static final boolean equals(char[] str1, char[] str2) {
        return Arrays.equals(str1, str2);
    }

    public static final boolean equals(char[][] strarr1, char[][] strarr2) {
        if (strarr1.length != strarr2.length) {
            return false;
        }
        for (int i = 0; i < strarr2.length; i++) {
            if (!Arrays.equals(strarr1[i], strarr2[i])) {
                return false;
            }
        }
        return true;
    }

    /**
     * Returns {@code true} if the contents of a character array are the same as contents
     * of a string.
     * @since 5.4
     */
    public static final boolean equals(char[] str1, String str2) {
        int length = str1.length;
        if (str2.length() != length)
            return false;

        for (int i = 0; i < length; i++) {
            if (str1[i] != str2.charAt(i))
                return false;
        }
        return true;
    }

    /**
     * Returns {@code true} if the contents of a section of a character array are the same as
     * contents of a string.
     * @since 5.5
     */
    public static final boolean equals(char[] str1, int start1,
            int length1, String str2) {
        if (length1 != str2.length() || str1.length < length1 + start1)
            return false;
        for (int i = 0; i < length1; ++i) {
            if (str1[start1++] != str2.charAt(i))
                return false;
        }
        return true;
    }

    /**
     * Returns {@code true} if the contents of a section of a character array are the same as
     * contents of another character array.
     */
    public static final boolean equals(char[] str1, int start1,
            int length1, char[] str2) {
        if (length1 != str2.length || str1.length < length1 + start1)
            return false;
        if (str1 == str2 && start1 == 0)
            return true;
        for (int i = 0; i < length1; ++i) {
            if (str1[start1++] != str2[i])
                return false;
        }

        return true;
    }

    public static final boolean equals(char[] str1, int start1,
            int length1, char[] str2, boolean ignoreCase) {
        if (!ignoreCase)
            return equals(str1, start1, length1, str2);

        if (length1 != str2.length || str1.length < start1 + length1)
            return false;

        for (int i = 0; i < length1; ++i) {
            if (Character.toLowerCase(str1[start1++]) != Character
                    .toLowerCase(str2[i]))
                return false;
        }
        return true;
    }
}

Related

  1. arrayIndexOf(Object[] array, Object element)
  2. arrayIndexOffset(int index, long baseOffset, long indexScale)
  3. indexOf(byte[] array, byte[] target, int fromIndex)
  4. indexOf(byte[] array, byte[] target, int start)
  5. indexOf(byte[] target, byte[] key)
  6. indexOf(final String[] p_elements, final String p_key)
  7. indexOf(final T[] array, final T value)
  8. indexOf(int[] arr, int e)
  9. indexOf(int[] arr, int val)