Vector receiving a function that returns integers

Asked

Viewed 174 times

2

Guys, I have some questions about vectors and functions.

public class teste{

/*Função que retorna um vetor de inteiros com números aleatórios*/

public static int [] vetorFuncao(){

 int numerosAleatorios [] = new int[10];

 for(int i=0;i<10;i++){

     numerosAleatorios[i] = ( (int) (Math.random()*10) )+1;
    }   

    return numerosAleatorios;

}

/*Principal*/

public static void main(String[]args){

int [] vetor = vetorFuncao();

     }

}

I know that to create a vector you must inform the amount of positions. Ex: int [] vetor = new int [10];

However, int [] vetor = vetorFuncao(); does not need to specify the amount of position, as the number of positions will be equal to the number of positions of the vetorFuncao();

Why you don’t need to specify the amount of vector positions int [] vetor = vetorFuncao() when I create a vector that receives a given function that returns an integer vector.

int [ ] vetor = new int [x]; Need to specify x;

int [] vetor = vetorFuncao(); No need to specify, because vetor.lenght will equal to vetorFuncao();

2 answers

0

There is a "subtle" difference between the two forms you used;

The first was the creation of a vector, at that moment, for the creation, it is necessary to declare the number of positions, because it will be an initial vector with all its empty positions, and for that it is necessary to indicate how many will be.

int[] vetor = new int[10];

In the second form, which is assignment, it is not necessary because the variable will only receive the value returned by a function, not needing to be defined its size, because as it is the return of something, the size will be defined based on the quantity returned by the function;

One is initialization vector, and in the other is attribution.

  • Thank you very much!

0


We’ll rearrange your code:

public class Teste {

    public static int[] vetorFuncao() {

        int[] numerosAleatorios = new int[10];

        for (int i = 0; i < 10; i++) {
            numerosAleatorios[i] = ((int) (Math.random() * 10)) + 1;
        }

        return numerosAleatorios;
    }

    public static void main(String[] args) {
        int[] vetor = vetorFuncao();
    }
}

First, pay attention to new. This keyword says something will be created. When you make a new int[10] you are creating an array with 10 positions. The part of the numerosAleatorios = says that this array will be assigned to the variable numerosAleatorios, who’s kind int[]. So the line int[] numerosAleatorios = new int[10]; does that:

  • Declares a type variable int[] calling for numerosAleatorios.
  • Creates an integer array of 10 positions.
  • Assigns this newly created array to the variable numerosAleatorios.

Further down, still within the method vetorFuncao() you have the return numerosAleatorios;. This statement returns the array numerosAleatorios as a result of the method that called.

Within the method main(String[]), when you call vetorFuncao(), you are invoking the method vetorFuncao(). The method vetorFuncao() will return the array it created as a result. When you have the int[] vetor = vetorFuncao();, this array that is created within vetorFuncao() will be assigned to the variable vetor. The main will not create by itself any array, it will only receive the array that vetorFuncao() took charge of creating.

In this case, the main is relying on the method vetorFuncao() the creation of the array, including its size. Inside the vetorFuncao(), the size is set to 10, and it is this array that will be returned. If you want to set the size on main, just use a parameter:

public class Teste {

    public static int[] vetorFuncao(int tamanho) {

        int[] numerosAleatorios = new int[tamanho];

        for (int i = 0; i < tamanho; i++) {
            numerosAleatorios[i] = ((int) (Math.random() * 10)) + 1;
        }

        return numerosAleatorios;
    }

    public static void main(String[] args) {
        int[] vetor = vetorFuncao(10);
    }
}

In this case, the method vetorFuncao "asks" who will use it the size of the array that will be created, and this size is directly passed to new. The main reports this value as 10 in the vetorFuncao.

  • Thank you very much!

  • @Raphaelribeiro If this answer solved your problem, mark it as accepted by clicking on the " " that is next to it. This will also mark your question as solved/solved. If you are still not satisfied and have any doubts or objections, feel free to comment. If you prefer to mark the other answer, feel free, although you can only choose one of them as you accept.

Browser other questions tagged

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