Java 在数组中查找对应数值的索引

查找

在数组中查找对应数值的索引

public class Seek{
    public static void main(String[] args) {
        int [] arr = {1,2,3};

        int y = Seek(arr,1);
        System.out.println(y);
    }
    public static int Seek(int[] arr,int number){
        for (int index = 0;index<arr.length;index++) {
            if (arr[index] == number) {
            //找到数值,返回索引
                return index;
            }
          //没找到数值,返回-1
        }return -1;
    }
}

发布了10 篇原创文章 · 获赞 0 · 访问量 322

猜你喜欢

转载自blog.csdn.net/weixin_44245615/article/details/104802911