Show a missing address

Asked

Viewed 53 times

2

I am doing some algorithms in various languages and in Java I come across a problem, in my function researcher when the value is not found in the list it should return me a None or null, but I can’t get one of the two, but if I order him to return 0 it works, but this wrong, I don’t want him to return it, I want him to warn me that the value entered in the item.

public class PesquisaBinaria {

public static void main(String[] args) {

    int[] minhaLista = { 1, 3, 5, 7, 9 };

    for (int i : minhaLista) {
        System.out.print(i + " ");
    }
    System.out.println(" ");
    System.out.println("Procurando endereço do número o 3 ~: " + pesquisa_binaria(minhaLista, 3));
    System.out.println("Procurando endereço do número o -1 ~:" + pesquisa_binaria(minhaLista, -1));
}

public static int pesquisa_binaria(int lista[], int item) {
    for (int i = 0; i < lista.length; i++) {

        int baixo = 0;
        int alto = lista.length - 1;

        while (baixo <= alto) {
            int meio = (baixo + alto) / 2;
            int chute = lista[meio];

            if (chute == item) {
                return meio;
            } else if (chute > item) {
                alto = meio - 1;
            } else {
                baixo = meio + 1;
            }
        }
    }
    return 0;
} }

what returns

    1 3 5 7 9  
Procurando endereço do número o 3 ~: 1
Procurando endereço do número o -1 ~:0

I say None because in Python I used it in case the item is not found

  • 1

    Your function cannot return null because their return is of the primitive type int. If you want to return null, change the function to return one Integer, then yes, you can return a null.

  • I believe it is because you are using primitive variables and they do not accept null try to use Wrappers, if you do not know what it is, see this link Wrappers in java

2 answers

3

You have to use Integer, not the primitive type int and yes the Wrapper, to be able to return a null, then your code will look like this:

public class Main {

public static void main(String[] args) {
    int[] minhaLista = { 1, 3, 5, 7, 9 };

    for (int i : minhaLista) {
        System.out.print(i + " ");
    }
    System.out.println(" ");
    System.out.println("Procurando endereço do número o 3 ~: " + pesquisaBinaria(minhaLista, 3));
    System.out.println("Procurando endereço do número o -1 ~:" + pesquisaBinaria(minhaLista, -1));

}

//Troquei o nome para ficar na convenção Java
public static Integer pesquisaBinaria(int lista[], int item) {
    for (int i = 0; i < lista.length; i++) {

        int baixo = 0;
        int alto = lista.length - 1;

        while (baixo <= alto) {
            int meio = (baixo + alto) / 2;
            int chute = lista[meio];

            if (chute == item) {
                return meio;
            } else if (chute > item) {
                alto = meio - 1;
            } else {
                baixo = meio + 1;
            }
        }
    }
    return null;
}

}

Console:

1 3 5 7 9  
Procurando endereço do número o 3 ~: 1
Procurando endereço do número o -1 ~:null

Sobre Wrappers:

Wrapper are known in the Java language as special classes that have methods capable of converting to primitive variables and also of encapsular primitive types to be worked as objects, ie, is made a bundle of streams that are flow of data through channels.

Therefore, there is a Wrapper class for each primitive type identified by the same name as the type it has and having the first uppercase letter. This declaration rule applies to all types, except those that are char classified as Character and Boolean as Boolean.

Source: http://www.linhadecodigo.com.br/artigo/3667/classes-wrappers-em-java.aspx

2

The guy int is a primitive type and cannot return null.

Change the return of your original function:

public static int pesquisa_binaria(int lista[], int item)

To:

public static Integer pesquisa_binaria(int lista[], int item)

Now you can return null in the logic of its function. Integer refers to an object now, and can return null.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.