集册 Java实例教程 查找数组中元素的第一个匹配项。

查找数组中元素的第一个匹配项。

欢马劈雪     最近更新时间:2020-01-02 10:19:05

424
查找数组中元素的第一个匹配项。

// Copyright (c) 2003-present, Jodd Team (jodd.org). All Rights Reserved.

import java.lang.reflect.Array;

import static jodd.util.StringPool.NULL;

/**
N o  w  J a v a . c o m - 时  代  Java
**/

public class Main{

    public static void main(String[] argv) throws Exception{

        byte[] array = new byte[]{34,35,36,37,37,37,67,68,69};

        byte value = 2;

        System.out.println(indexOf(array,value));

    }

    /**

     * Finds the first occurrence of an element in an array.

     */

    public static int indexOf(byte[] array, byte value) {

        for (int i = 0; i < array.length; i++) {

            if (array[i] == value) {

                return i;

            }

        }

        return -1;

    }

    /**

     * Finds the first occurrence of given value in an array from specified given position.

     *///来 自 N  o w  J a v a . c o m

    public static int indexOf(byte[] array, byte value, int startIndex) {

        for (int i = startIndex; i < array.length; i++) {

            if (array[i] == value) {

                return i;

            }

        }

        return -1;

    }

    /**

     * Finds the first occurrence in an array from specified given position and upto given length.

     */

    public static int indexOf(byte[] array, byte value, int startIndex,

            int endIndex) {

        for (int i = startIndex; i < endIndex; i++) {

            if (array[i] == value) {

                return i;

            }

        }

        return -1;

    }

    /**

     * Finds the first occurrence of an element in an array.

     */

    public static int indexOf(char[] array, char value) {

        for (int i = 0; i < array.length; i++) {

            if (array[i] == value) {

                return i;

            }

        }

        return -1;

    }

    /**

     * Finds the first occurrence of given value in an array from specified given position.

     */

    public static int indexOf(char[] array, char value, int startIndex) {

        for (int i = startIndex; i < array.length; i++) {

            if (array[i] == value) {

                return i;

            }

        }

        return -1;

    }

    /**

     * Finds the first occurrence in an array from specified given position and upto given length.

     */

    public static int indexOf(char[] array, char value, int startIndex,

            int endIndex) {

        for (int i = startIndex; i < endIndex; i++) {

            if (array[i] == value) {

                return i;

            }

        }

        return -1;

    }

    /**

     * Finds the first occurrence of an element in an array.

     */

    public static int indexOf(short[] array, short value) {

        for (int i = 0; i < array.length; i++) {

            if (array[i] == value) {

                return i;

            }

        }

        return -1;

    }

    /**

     * Finds the first occurrence of given value in an array from specified given position.

     */

    public static int indexOf(short[] array, short value, int startIndex) {

        for (int i = startIndex; i < array.length; i++) {

            if (array[i] == value) {

                return i;

            }

        }

        return -1;

    }

    /**

     * Finds the first occurrence in an array from specified given position and upto given length.

     */

    public static int indexOf(short[] array, short value, int startIndex,

            int endIndex) {

        for (int i = startIndex; i < endIndex; i++) {

            if (array[i] == value) {

                return i;

            }

        }

        return -1;

    }

    /**

     * Finds the first occurrence of an element in an array.

     */

    public static int indexOf(int[] array, int value) {

        for (int i = 0; i < array.length; i++) {

            if (array[i] == value) {

                return i;

            }

        }

        return -1;

    }

    /**

     * Finds the first occurrence of given value in an array from specified given position.

     */

    public static int indexOf(int[] array, int value, int startIndex) {

        for (int i = startIndex; i < array.length; i++) {

            if (array[i] == value) {

                return i;

            }

        }

        return -1;

    }

    /**

     * Finds the first occurrence in an array from specified given position and upto given length.

     */

    public static int indexOf(int[] array, int value, int startIndex,

            int endIndex) {

        for (int i = startIndex; i < endIndex; i++) {

            if (array[i] == value) {

                return i;

            }

        }

        return -1;

    }

    /**

     * Finds the first occurrence of an element in an array.

     */

    public static int indexOf(long[] array, long value) {

        for (int i = 0; i < array.length; i++) {

            if (array[i] == value) {

                return i;

            }

        }

        return -1;

    }

    /**

     * Finds the first occurrence of given value in an array from specified given position.

     */

    public static int indexOf(long[] array, long value, int startIndex) {

        for (int i = startIndex; i < array.length; i++) {

            if (array[i] == value) {

                return i;

            }

        }

        return -1;

    }

    /**

     * Finds the first occurrence in an array from specified given position and upto given length.

     */

    public static int indexOf(long[] array, long value, int startIndex,

            int endIndex) {

        for (int i = startIndex; i < endIndex; i++) {

            if (array[i] == value) {

                return i;

            }

        }

        return -1;

    }

    /**

     * Finds the first occurrence of an element in an array.

     */

    public static int indexOf(boolean[] array, boolean value) {

        for (int i = 0; i < array.length; i++) {

            if (array[i] == value) {

                return i;

            }

        }

        return -1;

    }

    /**

     * Finds the first occurrence of given value in an array from specified given position.

     */

    public static int indexOf(boolean[] array, boolean value, int startIndex) {

        for (int i = startIndex; i < array.length; i++) {

            if (array[i] == value) {

                return i;

            }

        }

        return -1;

    }

    /**

     * Finds the first occurrence in an array from specified given position and upto given length.

     */

    public static int indexOf(boolean[] array, boolean value,

            int startIndex, int endIndex) {

        for (int i = startIndex; i < endIndex; i++) {

            if (array[i] == value) {

                return i;

            }

        }

        return -1;

    }

    /**

     * Finds the first occurrence of value in <code>float</code> array.

     */

    public static int indexOf(float[] array, float value) {

        for (int i = 0; i < array.length; i++) {

            if (Float.compare(array[i], value) == 0) {

                return i;

            }

        }

        return -1;

    }

    /**

     * Finds the first occurrence of given value in <code>float</code>

     * array from specified given position.

     */

    public static int indexOf(float[] array, float value, int startIndex) {

        for (int i = startIndex; i < array.length; i++) {

            if (Float.compare(array[i], value) == 0) {

                return i;

            }

        }

        return -1;

    }

    /**

     * Finds the first occurrence in <code>float</code> array from specified given position and upto given length.

     */

    public static int indexOf(float[] array, float value, int startIndex,

            int endIndex) {

        for (int i = startIndex; i < endIndex; i++) {

            if (Float.compare(array[i], value) == 0) {

                return i;

            }

        }

        return -1;

    }

    /**

     * Finds the first occurrence of value in <code>double</code> array.

     */

    public static int indexOf(double[] array, double value) {

        for (int i = 0; i < array.length; i++) {

            if (Double.compare(array[i], value) == 0) {

                return i;

            }

        }

        return -1;

    }

    /**

     * Finds the first occurrence of given value in <code>double</code>

     * array from specified given position.

     */

    public static int indexOf(double[] array, double value, int startIndex) {

        for (int i = startIndex; i < array.length; i++) {

            if (Double.compare(array[i], value) == 0) {

                return i;

            }

        }

        return -1;

    }

    /**

     * Finds the first occurrence in <code>double</code> array from specified given position and upto given length.

     */

    public static int indexOf(double[] array, double value, int startIndex,

            int endIndex) {

        for (int i = startIndex; i < endIndex; i++) {

            if (Double.compare(array[i], value) == 0) {

                return i;

            }

        }

        return -1;

    }

    /**

     * Finds the first occurrence in an array.

     */

    public static int indexOf(Object[] array, Object value) {

        for (int i = 0; i < array.length; i++) {

            if (array[i].equals(value)) {

                return i;

            }

        }

        return -1;

    }

    /**

     * Finds the first occurrence in an array from specified given position.

     */

    public static int indexOf(Object[] array, Object value, int startIndex) {

        for (int i = startIndex; i < array.length; i++) {

            if (array[i].equals(value)) {

                return i;

            }

        }

        return -1;

    }

    /**

     * Finds the first occurrence in an array.

     */

    public static int indexOf(byte[] array, byte[] sub) {

        return indexOf(array, sub, 0, array.length);

    }

    /**

     * Finds the first occurrence in an array from specified given position.

     */

    public static int indexOf(byte[] array, byte[] sub, int startIndex) {

        return indexOf(array, sub, startIndex, array.length);

    }

    /**

     * Finds the first occurrence in an array from specified given position and upto given length.

     */

    public static int indexOf(byte[] array, byte[] sub, int startIndex,

            int endIndex) {

        int sublen = sub.length;

        if (sublen == 0) {

            return startIndex;

        }

        int total = endIndex - sublen + 1;

        byte c = sub[0];

        mainloop: for (int i = startIndex; i < total; i++) {

            if (array[i] != c) {

                continue;

            }

            int j = 1;

            int k = i + 1;

            while (j < sublen) {

                if (sub[j] != array[k]) {

                    continue mainloop;

                }

                j++;

                k++;

            }

            return i;

        }

        return -1;

    }

    /**

     * Finds the first occurrence in an array.

     */

    public static int indexOf(char[] array, char[] sub) {

        return indexOf(array, sub, 0, array.length);

    }

    /**

     * Finds the first occurrence in an array from specified given position.

     */

    public static int indexOf(char[] array, char[] sub, int startIndex) {

        return indexOf(array, sub, startIndex, array.length);

    }

    /**

     * Finds the first occurrence in an array from specified given position and upto given length.

     */

    public static int indexOf(char[] array, char[] sub, int startIndex,

            int endIndex) {

        int sublen = sub.length;

        if (sublen == 0) {

            return startIndex;

        }

        int total = endIndex - sublen + 1;

        char c = sub[0];

        mainloop: for (int i = startIndex; i < total; i++) {

            if (array[i] != c) {

                continue;

            }

            int j = 1;

            int k = i + 1;

            while (j < sublen) {

                if (sub[j] != array[k]) {

                    continue mainloop;

                }

                j++;

                k++;

            }

            return i;

        }

        return -1;

    }

    /**

     * Finds the first occurrence in an array.

     */

    public static int indexOf(short[] array, short[] sub) {

        return indexOf(array, sub, 0, array.length);

    }

    /**

     * Finds the first occurrence in an array from specified given position.

     */

    public static int indexOf(short[] array, short[] sub, int startIndex) {

        return indexOf(array, sub, startIndex, array.length);

    }

    /**

     * Finds the first occurrence in an array from specified given position and upto given length.

     */

    public static int indexOf(short[] array, short[] sub, int startIndex,

            int endIndex) {

        int sublen = sub.length;

        if (sublen == 0) {

            return startIndex;

        }

        int total = endIndex - sublen + 1;

        short c = sub[0];

        mainloop: for (int i = startIndex; i < total; i++) {

            if (array[i] != c) {

                continue;

            }

            int j = 1;

            int k = i + 1;

            while (j < sublen) {

                if (sub[j] != array[k]) {

                    continue mainloop;

                }

                j++;

                k++;

            }

            return i;

        }

        return -1;

    }

    /**

     * Finds the first occurrence in an array.

     */

    public static int indexOf(int[] array, int[] sub) {

        return indexOf(array, sub, 0, array.length);

    }

    /**

     * Finds the first occurrence in an array from specified given position.

     */

    public static int indexOf(int[] array, int[] sub, int startIndex) {

        return indexOf(array, sub, startIndex, array.length);

    }

    /**

     * Finds the first occurrence in an array from specified given position and upto given length.

     */

    public static int indexOf(int[] array, int[] sub, int startIndex,

            int endIndex) {

        int sublen = sub.length;

        if (sublen == 0) {

            return startIndex;

        }

        int total = endIndex - sublen + 1;

        int c = sub[0];

        mainloop: for (int i = startIndex; i < total; i++) {

            if (array[i] != c) {

                continue;

            }

            int j = 1;

            int k = i + 1;

            while (j < sublen) {

                if (sub[j] != array[k]) {

                    continue mainloop;

                }

                j++;

                k++;

            }

            return i;

        }

        return -1;

    }

    /**

     * Finds the first occurrence in an array.

     */

    public static int indexOf(long[] array, long[] sub) {

        return indexOf(array, sub, 0, array.length);

    }

    /**

     * Finds the first occurrence in an array from specified given position.

     */

    public static int indexOf(long[] array, long[] sub, int startIndex) {

        return indexOf(array, sub, startIndex, array.length);

    }

    /**

     * Finds the first occurrence in an array from specified given position and upto given length.

     */

    public static int indexOf(long[] array, long[] sub, int startIndex,

            int endIndex) {

        int sublen = sub.length;

        if (sublen == 0) {

            return startIndex;

        }

        int total = endIndex - sublen + 1;

        long c = sub[0];

        mainloop: for (int i = startIndex; i < total; i++) {

            if (array[i] != c) {

                continue;

            }

            int j = 1;

            int k = i + 1;

            while (j < sublen) {

                if (sub[j] != array[k]) {

                    continue mainloop;

                }

                j++;

                k++;

            }

            return i;

        }

        return -1;

    }

    /**

     * Finds the first occurrence in an array.

     */

    public static int indexOf(boolean[] array, boolean[] sub) {

        return indexOf(array, sub, 0, array.length);

    }

    /**

     * Finds the first occurrence in an array from specified given position.

     */

    public static int indexOf(boolean[] array, boolean[] sub, int startIndex) {

        return indexOf(array, sub, startIndex, array.length);

    }

    /**

     * Finds the first occurrence in an array from specified given position and upto given length.

     */

    public static int indexOf(boolean[] array, boolean[] sub,

            int startIndex, int endIndex) {

        int sublen = sub.length;

        if (sublen == 0) {

            return startIndex;

        }

        
展开阅读全文