数组元素查找(查找指定元素第一次在数组中出现的索引)

数组元素查找(查找指定元素第一次在数组中出现的索引):

class Hello2 {
    public static void main(String[] args) {
        int[] arr = {11,12,13,14,15};
        int index = where(arr,12);
        System.out.println(index);
    }
    public static int where(int[] arr,int a) {
        for (int i = 0;i < arr.length ;i++ )
        {
            if (arr[i] == a)
            {
                return i;
            }
        }
        return -1;
    }


}

结果:

原文地址:https://www.cnblogs.com/Wangzui1127/p/11191881.html