集册 Java实例教程 在数组中搜索元素,如果存在则返回其索引。

在数组中搜索元素,如果存在则返回其索引。

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

523
在数组中搜索元素,如果存在则返回其索引。
//from 时 代 J a v a

/*

 * Copyright (C) 2014 Riccardo Traverso

 * 

 * This file is part of JavaUtils

 * Website: https://github.com/rtraverso86/JavaUtils

 * 

 * JavaUtils is free software: you can redistribute it and/or modify 

 * it under the terms of the GNU Lesser General Public License as published by

 * the Free Software Foundation, either version 3 of the License, or

 * (at your option) any later version.

 * 

 * JavaUtils is distributed in the hope that it will be useful,

 * but WITHOUT ANY WARRANTY; without even the implied warranty of

 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the

 * GNU Lesser General Public License for more details.

 * 

 * You should have received a copy of the GNU Lesser General Public License

 * along with this program.  If not, see <http://www.gnu.org/licenses/>.

 */

//package com.nowjava;


public class Main {

    /**

     * Searches for an element in an array, returning its index if present. The

     * objects in the array are first compared by reference with the given

     * object, and then by means of the equals() method.

     * 

     * @param arr

     *            the array to search in

     * @param obj

     *            the object to search for in the given array

     * @return the index of the given object in the array if it is present, or

     *         -1 otherwise

     */

    public static <T> int find(T[] arr, T obj) {

        for (int idx = 0; idx < arr.length; idx++) {

            T t = arr[idx];

            if (t == obj || t.equals(obj)) {

                return idx;

            }

        }

        return -1;//来自 时 代 J a v a - N o w J a v a . c o m

    }


    /**

     * @see #find(Object[], Object)

     */

    public static int find(byte[] arr, byte b) {

        for (int idx = 0; idx < arr.length; idx++) {

            byte t = arr[idx];

            if (t == b) {

                return idx;

            }

        }

        return -1;

    }


    /**

     * @see #find(Object[], Object)

     */

    public static int find(short[] arr, short s) {

        for (int idx = 0; idx < arr.length; idx++) {

            short t = arr[idx];

            if (t == s) {

                return idx;

            }

        }

        return -1;

    }


    /**

     * @see #find(Object[], Object)

     */

    public static int find(boolean[] arr, boolean b) {

        for (int idx = 0; idx < arr.length; idx++) {

            boolean t = arr[idx];

            if (t == b) {

                return idx;

            }

        }

        return -1;

    }


    /**

     * @see #find(Object[], Object)

     */

    public static int find(char[] arr, char c) {

        for (int idx = 0; idx < arr.length; idx++) {

            char t = arr[idx];

            if (t == c) {

                return idx;

            }

        }

        return -1;

    }


    /**

     * @see #find(Object[], Object)

     */

    public static int find(int[] arr, int i) {

        for (int idx = 0; idx < arr.length; idx++) {

            int t = arr[idx];

            if (t == i) {

                return idx;

            }

        }

        return -1;

    }


    /**

     * @see #find(Object[], Object)

     */

    public static int find(long[] arr, long l) {

        for (int idx = 0; idx < arr.length; idx++) {

            long t = arr[idx];

            if (t == l) {

                return idx;

            }

        }

        return -1;

    }


    /**

     * @see #find(Object[], Object)

     */

    public static int find(
展开阅读全文