Vector getting a function that returns integers

2

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 one must inform the number of positions. Ex: int [] vetor = new int [10];

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

Why do not you need to specify the number of positions of the int [] vetor = vetorFuncao() vector when I create a vector that gets a given function that returns an integer vector.

int [ ] vetor = new int [x]; You need to specify x ;

int [] vetor = vetorFuncao(); Do not specify, since vetor.lenght will equal vetorFuncao();

    
asked by anonymous 14.12.2017 / 20:01

2 answers

0

Let's 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 . That keyword says something will be created. When you make a new int[10] you are creating a 10-position array. The numerosAleatorios = part says that this array is going to be assigned to the numerosAleatorios variable, which is of type int[] . Therefore, the int[] numerosAleatorios = new int[10]; line does this:

  • Declare a variable of type int[] called numerosAleatorios .
  • Creates an integer array with 10 positions.
  • Assign this newly created array to the variable numerosAleatorios .

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

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

In this case, main is relying on the vetorFuncao() method of creating the array, including its size. Within vetorFuncao() , the size is set to 10, and it is this array that will be returned. If you want to set the size in 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 vetorFuncao method "asks" the user to use the size of the array to be created, and this size is directly passed to new . The main reports this value as 10 in the vetorFuncao parameter.

    
14.12.2017 / 20:16
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, since it will be an initial vector with all its empty positions, and for this it is necessary to indicate how many will be. p>

int[] vetor = new int[10];

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

One is initialization of vector, and the other is assignment .

    
14.12.2017 / 20:13