集册 Java实例教程 在数组中查找值的索引。

在数组中查找值的索引。

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

471
在数组中查找值的索引。

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

//package com.nowjava;


public class Main {

    public static void main(String[] argv) {

        Object[] array = new String[] { "1", "abc", "level", null,

                "nowjava.com", "asdf 123" };

        Object value = "nowjava.com";

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

    }


    /**

     * Find the index of a value inside an array. If not found, or any of the param is null, return -1

     * @param array The array to lookup the value (if null, return -1)

     * @param value The value to lookup (if null, return -1)

     * @return the index of the match value, or -1 is not found

     */

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

        if (array == null || value == null) {

            return -1;//时 代 J a v a 公 众 号 - N o w J a v  a . c o m 提 供

        }

        int i = 0;

        
展开阅读全文