Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

92
Views
How to find if objects of an array are equals

the method equals is overriden but findEquals doesn't print "duplicate" even when i enter for every Egnine's new object same variables.

public boolean equals(Object o) {
    if (this == o) return true;
    if (o == null || getClass() != o.getClass()) return false;
    EngineStorage that = (EngineStorage) o;
    return Arrays.equals(engines, that.engines) && scanner.equals(that.scanner);
}

public void fillArray(Engine[] engines) {

    for (int i = 0; i < engines.length; i++) {
        System.out.println("Enter model");
        String name = scanner.nextLine();
        System.out.println("Enter volume");
        double vol = scanner.nextDouble();
        scanner.nextLine();
        engines[i] = new Engine(name, vol);
    }
}

public void findEquals(Engine [] engines) {
    for (int i = 0; i < engines.length - 1; i++) {
        if (engines[i].equals(engines[i + 1])) {
            System.out.println("duplikat");
            break;
        }
    }
}
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

It's not enough to override equals in EngineStorage class.

You should also override it in Engine class, so that Arrays.equals(Object[] a, Object[] a2) will use your implementation.

Besides, your findEquals method explicitly calls equals on Engine instances, which is another reason to override equals in that class.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!