Return vector of integers in java

2

I'm trying to read a vector of integers and return the amount of ones and the number of zeros, but it always returns at least a zero, even though I did not enter that value. Debugging the code, even at the time of reading, if I enter 5 times the number "one" it only shows how filled with the number one the first house, and yet, count four. The same thing for debugging in the method contains . I would like to know if my error is in the function that does the reading or in the function that counts, grateful.

import java.util.Scanner; //importa classe scanner para fazer a leitura do teclado

/**
 * link : https://www.urionlinejudge.com.br/judge/pt/problems/view/1329
 * @author pmargreff
 */
class Main {

    //método para ler o vetor
    public static int[] leVetor(int tamanho) {
        Scanner teclado = new Scanner(System.in);
        int[] vetor = new int[tamanho];
        for (int i : vetor) {
            vetor[i] = teclado.nextInt();
        }

        return vetor;
    }

    public static int contZero(int[] vetor) {
        int total = 0;
        for (int i: vetor){
            if (vetor[i] == 0)
                total++;
        }
        return total;
    }

    public static int contUm(int[] vetor) {
        int total = 0;
        for (int i: vetor){
            if (vetor[i] == 1)
                total++;
        }
        return total;
    }

    public static void main(String[] args) {
        Scanner teclado = new Scanner(System.in); //inicializa teclado como leitura padrão para entrada
        int tamanho = 1; // váriavel que conterá o tamanho total do vetor
        int vetor[]; //vetor onde irá ficar armazenado os resultados da jogada
        int zero, um; //contém o número de vitória referente a cada um

        while (tamanho != 0) {

            tamanho = teclado.nextInt(); //le o tamanho do vetor

            if (tamanho > 0) {
                vetor = new int[tamanho];   //inicializa variáveis com o espaço necessário na memória 
                vetor = leVetor(tamanho); //le o vetor e salve nele próprio
                zero = contZero(vetor);
                um = contUm(vetor);
                System.out.println("Zeros: " + zero);
                System.out.println("Uns: " + um);
            }
        }
    }
}
    
asked by anonymous 27.01.2015 / 02:58

1 answer

4

The problem is how you change the array. See:

for (int i : vetor) {
   vetor[i] = teclado.nextInt();
}

The above code is getting each value in the array and then using this value as the index. It turns out that array index is one thing and the value at that index is another. You have just initialized the array with integers X. Each integer ( int ) was initialized by Java with a value of zero. So in your first loop, you are setting X times the value in the zero index, each new value overwrites the previous one, always in the zero index.

Try to go through your arrays ("vectors") like this:

for (int i = 0; i < vetor.length; i++) {
    vetor[i] = teclado.nextInt();
}

In the above code the i value is incremented by each iteration in the loop and up to the last element in the array; and then i is used as an index to change the elements of this same array.

    
27.01.2015 / 03:12