集册 Java实例教程 指示数组中是否存在元素。

指示数组中是否存在元素。

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

577
指示数组中是否存在元素。
// 来 自 时 代 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 {

    /**

     * Tells whether an element is present in an array. 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 true iff the object is present in the given array

     */

    public static <T> boolean contains(T[] arr, T obj) {

        return find(arr, obj) >= 0;

    }


    /**

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

     */

    public static boolean contains(byte[] arr, byte obj) {

        return find(arr, obj) >= 0;

    }


    /**

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

     */

    public static boolean contains(short[] arr, short obj) {

        return find(arr, obj) >= 0;

    }
/* 来自 时 代      J a v a   公   众 号 - nowjava.com*/

    /**

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

     */

    public static boolean contains(boolean[] arr, boolean obj) {

        return find(arr, obj) >= 0;

    }


    /**

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

     */

    public static boolean contains(char[] arr, char obj) {

        return find(arr, obj) >= 0;

    }


    /**

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

     */

    public static boolean contains(int[] arr, int obj) {

        return find(arr, obj) >= 0;

    }


    /**

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

     */

    public static boolean contains(long[] arr, long obj) {

        return find(arr, obj) >= 0;

    }


    /**

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

     */

    public static boolean contains(float[] arr, float obj) {

        return find(arr, obj) >= 0;

    }


    /**

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

     */

    public static boolean contains(double[] arr, double obj) {

        return find(arr, obj) >= 0;

    }


    /**

     * 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;

    }


    /**

     * @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) {

        
展开阅读全文