Déterminer si le tableau contient une certaine valeur ou non

Utilisation selon Arrays.binarySearch () pour déterminer

Exemple de code:

 public static void main(String[] args) {
        int[] array = {1, 3, 4, 2, 5, 6, 0};
        System.out.println("排序前:" + Arrays.toString(array));
        Arrays.sort(array);
        System.out.println("排序后:" + Arrays.toString(array));
        System.out.println(Arrays.binarySearch(array, 0));
    }

Les résultats sont les suivants:
Insérer ici l'image Description
Retour à la recherche de la valeur d'index 0, ce qui indique que la valeur 0 est présent dans le tableau.

Pourquoi être ici méthode et ensuite appeler triés Arrays.binarySearch () il

Jetez un oeil à l'exemple de code n'est pas le genre:

 public static void main(String[] args) {
        int[] array = {1, 3, 4, 2, 5, 6, 0};
        System.out.println("排序前:" + Arrays.toString(array));
        System.out.println("排序后:" + Arrays.toString(array));
        System.out.println(Arrays.binarySearch(array, 0));
    }

Sortie:
Insérer ici l'image Description
Ici vous pouvez voir, il est clair qu'il ya un tableau 0, mais le retour est -1, la recherche non pas au nom de cette valeur, et ne correspond pas aux résultats escomptés.

Regardez Arrays.binarySearch () code source Analyse:

public static int binarySearch(int[] a, int key) {
        //真正调用的还是binarySearch0()方法
        return binarySearch0(a, 0, a.length, key);
    }
    
 private static int binarySearch0(int[] a, int fromIndex, int toIndex,
                                     int key) {
        //fromIndex默认传参就是0,所以low=0                     
        int low = fromIndex;
        //toIndex是数组的长度7,所以high=6
        int high = toIndex - 1;
        
        //循环判断
        while (low <= high) {
            //无符号右移1位,其实就是(low + high)/2,只是用移位运算更快 
            int mid = (low + high) >>> 1;
            //取得中间下标mid的数值
            int midVal = a[mid];
            //如果midVal值小于要搜索的key,则认为key在mid+1下标和high下标之间  
            if (midVal < key)
                //所以把low赋值成mid + 1
                low = mid + 1;
            //如果midVal值大于要搜索的key,则认为key在0下标和mid-1下标之间  
            else if (midVal > key)
                //所以把high赋值成mid - 1
                high = mid - 1;
            else
                //直到midVal等于key时,才退出循环,返回相应下标
                return mid; // key found
        }
        //如果实在是搜不到,就返回-(low + 1)
        return -(low + 1);  // key not found.
    }

De source peut savoir Arrays.binarySearch () utilise la dichotomie pour rechercher des données de positionnement, vous avez donc besoin de faire le tri, et une fois le rendement négatif, cela signifie aucune correspondance n'a été trouvée.

résumé

Voici les détermine si le partage réel tableau contient la valeur d'une autre méthode, des boucles parfois par le jugement de la comparaison, il peut être plus efficace dans la compréhension de la méthode Arrays.binarySearch () peut choisir une meilleure façon.

Publié 297 articles originaux · éloge gagné 311 · vues + 50000

Je suppose que tu aimes

Origine blog.csdn.net/weixin_38106322/article/details/105238071
conseillé
Classement