How to compare the content of two vectors?

Asked

Viewed 4,626 times

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 must be wrong?

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

  • Do you want to compare the values of both vectors? The way you are doing, if compares whether the reference to the two vector objects is the same, which is false. You need to do a for to rotate over the first vector and another for internal to compare all the indices of the second as the first for will iterate the indices of the first vector.

  • Yes, in vector case A[0]: 2 | vector B[0]: 2. if(vector A==vector B) C++. This is the idea.

2 answers

9

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

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

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

That returns something like this in the ideone:

[D@106d69c - [D@52e922

To compare the values, you can iterate between the indices of the two vectors using repetition loop (as shown in the @Weslleytavares response), or using the class 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(equal and different) here in the IDEONE

Note: Note that the method equals compares even-to-par vectors, and they will be the same if their memory references are null, or if have the same size, and have the same values for the same pair index.

3


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

Follows 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);
    }
}
  • What’s the difference between having and not having the brackets?

  • Since it is an array, square brackets are the delimiters of it. Thus, when calling the notaVetor[i] refers to the contents of that array position.

Browser other questions tagged

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