簡體   English   中英

在數組中查找數字的最佳做法是什么?

[英]What's best practice for finding a number in an array?

..還有,這到底是怎么回事?

    int [] numbers1To9 = new int[]{1,2,3,4,5,6,7,8,9};
    System.out.println("one is here, true or false?: "+Arrays.asList(numbers1To9).contains(1));

output:一個在這里,是真是假?:假

在數組中查找數字的最佳方法取決於您的數組,是否已排序

如果您的數組已排序,那么您將能夠在 O(log(N)) 的時間運行中找到數組中的 num。

但是,如果您的數組未排序,那么您將需要考慮對數組進行排序,然后有幾種算法可以在數組中找到您的數字。

比如二分查找等等...

如果使用已排序的 arrays,或者當對未排序數組的排序操作可以被認為是“廉價”時, binarySearch可以被認為是一個不錯的選擇; 它避免了創建更多的 collections(例如Lists ),因為它直接與原始數組一起使用,並且其機制旨在找到存儲所需密鑰的 position(或其中之一)。 結果,您能夠識別它的存在(隱式)和所在的索引。

您已經對數組進行了排序,因此在您的情況下不需要(這是使用此算法的優勢); 請注意,在使用未排序的 arrays 時,必須在binarySearch之前調用Arrays.sort以避免出現“未定義”結果。


例如,如果您想知道值1是否存在:

    /*Arrays.sort(numbers1To9); -- only if unsorted*/
    boolean found = (Arrays.binarySearch((numbers1To9), 1))>=0?true:false; //--> true

例如,如果您還希望獲得值2的 position:

    /*Arrays.sort(numbers1To9); -- only if unsorted*/
    int pos = Arrays.binarySearch((numbers1To9), 2); //-->1
    boolean found = pos>=0; //--> true

如果未找到該元素, binarySearch將僅返回負數 output。 如果要找到的密鑰重復,則無法保證指定密鑰中的哪個 position 會返回。

不管怎樣,如果結果>=0 ,數組保證包含數字,並且所需的值也保證存儲在返回的索引中。


找不到密鑰時的結果有點有趣

如果未找到密鑰,則顯示的否定結果遵循以下邏輯:

(-(插入點) - 1) . 插入點定義為將鍵插入數組的點:大於鍵的第一個元素的索引,如果數組中的所有元素都小於指定的鍵,則為 a.length 這保證了當且僅當找到密鑰時返回值 >= 0。

因此,如果您嘗試找到任何大於 9 的數字,插入點將是numbers1To9.length -> 9 因此, 10INTEGER.MAX_VALUE將 output 與 position 相同:

int pos = Arrays.binarySearch((numbers1To9), 10);                // -(9)-1 --> pos=-10
    pos = Arrays.binarySearch((numbers1To9), Integer.MAX_VALUE); // -(9)-1 --> pos=-10

對於數字 0,插入點將為0因為 1 更大,其在數組中的 position 為 0 ):

int pos = Arrays.binarySearch((numbers1To9), 0); // -(0)-1 --> pos=-1

為了查看 binarySearch 對未排序的 arrays的處理效果如何

    int [] numberUnsorted= new int[]{1,2,4,9,7,6,5,8,3};
    int pos = Arrays.binarySearch((numberUnsorted), 3); //--> pos = -3  (FAIL)
        pos = Arrays.binarySearch((numberUnsorted), 9); //--> pos = -10 (FAIL)
        pos = Arrays.binarySearch((numberUnsorted), 6); //--> pos = -4  (FAIL)

所以稱它們為“未定義”是一個非常仁慈的熱


請注意, binarySearch 將是查找的“最佳實踐之一”
數組中的一個數字,條件是數組被排序 在其他情況下,數組未排序,您可能會意識到對其進行排序的復雜性,並決定是否需要另一種不需要排序操作的機制是更好的方法。 這將取決於數組類型、大小和值。 在不知道搜索的具體上下文的情況下,通常沒有“最佳確定方法”

除了自己在 int[] 中查找數字,並且接近您自己的嘗試,您可以相當容易地將 int[] 轉換為 List:

int [] numbers1To9 = new int[]{1,2,3,4,5,6,7,8,9};
List<Integer> ilist = IntStream.of(numbers1To9).boxed().collect(Collectors.toList());
System.out.println("one is here, true or false?: "+ ilist.contains(1));

Output: one is here, true or false?: true

編輯

正如下面評論中提到的,如果您的唯一目標是檢查某個值是否存在,則根本不需要裝箱和收集:

boolean contains1 = IntStream.of(numbers1To9).anyMatch(i -> i == 1);
System.out.println("one is here, true or false?: "+ contains1);

..還有,這里到底發生了什么?

    int [] numbers1To9 = new int[]{1,2,3,4,5,6,7,8,9};
    System.out.println("one is here, true or false?: "+Arrays.asList(numbers1To9).contains(1));

output:一個在這里,是真是假?:假

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM