How to compare the contents of two vectors?

5
public class VetorTurma {

    public static void main(String[] args) {

        int pontuacao = 0,nota,c;
        String nome;  

            Scanner sc = new Scanner(System.in);

            double gabaritoVetor[] = new double[10];
            double notaVetor[] = new double[10]; 

            System.out.println("Digite o gabarito: ");
            for(c=0;c < gabaritoVetor.length;c++){
                System.out.print("Gabarito | questão["+c+"]: ");
            gabaritoVetor[c]=sc.nextDouble();


            }
            int i = 0;
            for(i=0;i < notaVetor.length;i++){
                System.out.print("Questão["+i+"]: ");
                notaVetor[i] = sc.nextDouble();



            }

            if(gabaritoVetor==notaVetor){
                 pontuacao ++;


            }

            System.out.println("Pontuação: "+pontuacao);
     }
    }

The if counter always returns zero. What should be wrong?

NOTE: It might seem strange numbers as template the right would be a, b, c, d. But first I want to do with numbers and then as characters.

    
asked by anonymous 14.02.2016 / 02:35

2 answers

3

I believe that with this adjustment, your score can be calculated effectively.

Follow the solution:

import java.util.Scanner;

public class VetorTurma {

    public static void main(String[] args) {

        int pontuacao = 0, nota;
        String nome;

        Scanner sc = new Scanner(System.in);

        int gabaritoVetor[] = new int[3];
        int notaVetor[] = new int[3];

        System.out.println("Digite o gabarito: ");
        for (int c = 0; c < gabaritoVetor.length; c++) {
            System.out.print("Gabarito | questão[" + c + "]: ");
            gabaritoVetor[c] = sc.nextInt();

        }
        for (int i = 0; i < notaVetor.length; i++) {
            System.out.print("Questão[" + i + "]: ");
            notaVetor[i] = sc.nextInt();
            if (notaVetor[i] == gabaritoVetor[i])
                pontuacao++;
        }

        System.out.println("Pontuação: " + pontuacao);
    }
}
    
14.02.2016 / 02:56
9

When you do if(gabaritoVetor==notaVetor) , you are comparing only the reference of both vectors in memory, and because they are objects allocated in different spaces, the comparison will result in false and will not increase. See the example:

double vetor1[] = new double[3]; 
double vetor2[] = new double[3];

        System.out.print(vetor1 + " - " + vetor2);

That something like this returns in ideone :

  

[D @ 106d69c - [D @ 52e922]

To compare the values, you can iterate between the indices of the two vectors using loop repetition (as shown in @WeslleyTavares's response), or using the Arrays :

 if(Arrays.equals(vetor1,vetor2)){
            System.out.println("vetor1  e vetor2 iguais");
        }else{
            System.out.print("vetor1  e vetor2 diferentes");
        }

See a full comparison test using Arrays between vectors (same and different) here at IDEONE

  

Note: Note that the equals " compares the vectors together, and   they will be equal if their references in memory are null, or if they are   have the same size, and have the same values for the same pair   of indexes.

    
14.02.2016 / 03:06