查找算法之线性查找

package TestOne.dog;

import TestOne.panda.Search;

public class TestSearch {
    public static void main(String[] args) {
        int[] arr = new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 23, 3, 32, 34, 4, 5, 5, 5, 6, 6, 56, 7, 7, 6};
        int flag = 23;
        new Search(flag, arr);
    }
}

package TestOne.panda;

public class Search {
    public Search(int flag, int[] arr) {
        int index = -1;
        int i = 0;
        for (i = 0; i < arr.length; i++) {
            if (flag == arr[i]) {
                index = i;
                break;
            }
        }
        if (index == -1) {
            System.out.println("该数组中没有所需查找的数");
        } else {
            System.out.println("index: " + index);
        }
    }
}


发布了55 篇原创文章 · 获赞 15 · 访问量 7万+

猜你喜欢

转载自blog.csdn.net/qq_43141611/article/details/105109610