Difficulties with Array Class binarySearch method

2

I created two arrays one of integers and another of Strings (objects), but when I used the binarySearch method to know the position of the elements, the return of the positions of the Strings array went differently, why did this happen?

showing code and return.

import java.util.Arrays;
public class ArraySimples {
    public static void main(String[] args) {
        String [] paises ={"Brasil", "Russia", "India", "China", "Argentina","Paraguai"};
        int [] numeros = {5,7,9,11,13};
        int posicao0 =Arrays.binarySearch(paises, "Brasil");
        int posicao1 =Arrays.binarySearch(paises, "Russia");
        int posicao2 =Arrays.binarySearch(paises, "India");
        int posicao3 =Arrays.binarySearch(paises, " China");
        int posicao4 =Arrays.binarySearch(paises, "Argentina");
        int posicao5 =Arrays.binarySearch(paises, "Paraguai");

        System.out.println("Brasil: " + posicao0);
        System.out.println("Russia: " + posicao1);
        System.out.println("India: " + posicao2);
        System.out.println("China: " + posicao3);
        System.out.println("Argentina:" + posicao4);
        System.out.println("Paraguai" + posicao5);

        int posicao00 =Arrays.binarySearch(numeros, 5);
        int posicao11 =Arrays.binarySearch(numeros, 7);
        int posicao22 =Arrays.binarySearch(numeros, 9);
        int posicao33 =Arrays.binarySearch(numeros, 11);
        int posicao44 =Arrays.binarySearch(numeros, 13);
        System.out.println("5: " + posicao00);
        System.out.println("7: " + posicao11);
        System.out.println("9: " + posicao22);
        System.out.println("11: " + posicao33);
        System.out.println("13: " +posicao44);
    }
}

    
asked by anonymous 15.04.2014 / 07:07

1 answer

5

To use binarySearch , you must first sort the Array:

java.util.Arrays.sort();

See this excerpt from Java documentation :

  

The array must be sorted into ascending order according to the natural ordering of its elements (as by the sort (Object []) method prior to making this call. If it is not sorted, the results are undefined.

Translating:

  

The array must be organized in ascending order according to the natural order of its elements (as by the sort method (Object []) before making this call.

Notice that because your test numbers were already sorted in source, the result was expected. Countries, in turn, are not in the natural order. If they were in order too, we probably would not realize a potential problem.

The solution applied to your code is simple:

import java.util.Arrays;

public class ArraySimples {
   public static void main(String[] args) {

   String [] paises ={"Brasil", "Russia", "India", "China", "Argentina", "Paraguai"};

      Arrays.sort(paises);

      int posicao0 = Arrays.binarySearch(paises, "Brasil");
      int posicao1 = Arrays.binarySearch(paises, "Russia");
      int posicao2 = Arrays.binarySearch(paises, "India");
      int posicao3 = Arrays.binarySearch(paises, "China");
      int posicao4 = Arrays.binarySearch(paises, "Argentina");
      int posicao5 = Arrays.binarySearch(paises, "Paraguai");

      System.out.println("Brasil: " + posicao0);
      System.out.println("Russia: " + posicao1);
      System.out.println("India: " + posicao2);
      System.out.println("China: " + posicao3);
      System.out.println("Argentina: " + posicao4);
      System.out.println("Paraguai: " + posicao5);
   }
}
    
15.04.2014 / 07:21