简体   繁体   中英

Java: How to compare every two elements in an array once?

This is an assignment we have and I'm a little bit lost with his directions. He tells us to write a program with a method called equals to compare the salary of EVERY TWO EMPLOYEES in the array (I've already created this) and prints the SSN of every two employees that have the same salary in pairs. He made a disclaimer that the code should only compare two different employees and only once. Every two different employees are compared only once. The array is size 10.

I've already asked multiple peers on what he meant but they are also confused. This is what I have: For the Class:

    public boolean equals(Employee e)
    {
        boolean status;
        if(salary == e.getSalary())
        {
           status = true;
        }
        else
           status = false;
        return status;
    }

For the Demo:

     for(int i = 0; i < employees.length; i++)
     {
        for(int j = i+1; j < employees.length; j++)
        {
           if(employees[i].equals(employees[j]))
              System.out.println(employees[i].getSsn() + "\t" + 
                                 employees[j].getSsn());
        }
     }

It compiles, however it skips comparing index 0 and index 1. Any help is appreciated.

int j = i+1而不是int j = i+2初始化j

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