简体   繁体   中英

Compare two vectors in java?

I want to compare these two vectors so that it will return how many Patients are also students. I was given the main and i've added to the HistAndMarks method but it doesnt seem to work. The main confusion is with the HistAndMarks method and how i would compare two vectors.

Thanks

import java.util.*;
public class Ex6 {

    public static int count = 0;

     public static void main(String[] a)
     {
        Vector<Patient>  P = new Vector<Patient>();
        Vector<Student>  S = new Vector<Student>();
       int i=Integer.parseInt(a[0]);
       if (i==0) {
          P.add(new Patient("Jimmy1","1","d"));
          P.add(new Patient("Jimmy2","1","d"));
          P.add(new Patient("Jimmy3","1","d"));
          P.add(new Patient("Jimmy","1","d"));
          S.add(new Student("Jimmy1","1",null) );
          S.add(new Student("Jimmy2","1",null) );
          S.add(new Student("Jimmy3","1",null) );
          S.add(new Student("Lisa","1",null) );
          System.out.println(new Ex6().HistAndMarks(P,S));
        }
         if (i==1) {
          P.add(new Patient("Jimmy","1","d"));
          S.add(new Student("Jimmy1","1",null) );
           System.out.println(new Ex6().HistAndMarks(P,S));
        }
        if (i==2)  System.out.println(new Ex6().HistAndMarks(P,null));
    }

     public static int HistAndMarks (Vector<Patient>  P, Vector<Student>  S) {

        for(int i = 0; i > P.size(); i++){
            for(int z = -1; z > S.size(); z++){
                Patient Pnew = P.get(i);
                Student Snew = S.get(z + 1);
                if(P.contains(S)){
                    count = count + 1;

                }
            }
        }
       return count;
    }
}

I'd start by looking at

if(P.contains(S)){

This is comparing the two vectors, not the individual elements within them. It's effectively saying is Vector S in Vector P - this is not what you are wanting to check.

You have isolated Pnew and Snew - these are to two objects that you want to compare with each other. In order to compare them, you need to have a rule that indicates whether the two elements are actually the same person. Simplistically, this could be if the name of the person that Pnew represents as the same as the name of the person the Snew represents, then the two can be considered to be the same person

The problem is that you compare invalid object

if(P.contains(S)){
  count = count + 1;
}

Probably should be

if(P.contains(Snew)){
  count = count + 1;
}

Note that you do not have to iterate through both list:

public static int histAndMarks(Vector<Patient>  patients, Vector<Student>  students) {

    int count = 0; 

    for(Patient patient : patients) { //Fetch next patient
       if(students.contains(patient) { //Check that students contains patient 
          count++; //Add one to counter
       }
    }

    return count;
}

Note that the Vetctor class is very old and not common to use. You may want to switch to List

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM