0

How I can compare each object in an array to identify if they are the same; ex. compare a hockey player to a golfer and output they are not the same, or compare two hockey players to each other and output that they are the same.

I am unsure how to implement this.

players[0] = new BaseballPlayer(...stuff);
players[1] = new FootBall(...stuff);
players[2] = new HockeyPlayer(...stuff);
players[3] = new Golfer(...stuff);
players[4] = new BaseballPlayer(...stuff);
players[5] = new FootBall(...stuff);
players[6] = new HockeyPlayer(...stuff);
players[7] = new Golfer(...stuff);
players[8] = new BaseballPlayer(...stuff);
players[9] = new FootBall(...stuff);
players[10] = new HockeyPlayer(...stuff);
players[11] = new Golfer(...stuff);
players[12] = new BaseballPlayer(...stuff);
players[13] = new FootBall(...stuff);
players[14] = new HockeyPlayer(...stuff);
players[15] = new Golfer(...stuff);
Jake
  • 59
  • 5

2 Answers2

0

Utilities to implement this have already been provided in the JDK. Please look at Arrays documentation, specifically deepEquals for verifying nested arrays.

jon5477
  • 520
  • 6
  • 17
  • 1
    `Arrays.equals()` (and `deepEquals()`) still requires the objects in the array to have `equals()` implemented correctly. Also, you misunderstand the question. It has nothing to do with arrays being equal. – Kayaman Nov 20 '17 at 20:18
0

See Arrays.equals(Object[], Object[]) and implement equals() (and hashCode()) on your base class (I'm assuming all players inherit from the same Player class).

Matthieu
  • 2,736
  • 4
  • 57
  • 87
  • so lets say: public static boolean equals(players[], players[]) { if (true) { System.out.println("related"); } else System.out.println("not related"); } – Jake Nov 20 '17 at 20:26
  • ^ does not compile not sure how to fix it. – Jake Nov 20 '17 at 20:27
  • @Jack, either hand-code the static method, or override `equals()` and `hashCode()` inside each class definition (or base-class, whichever makes more sense). – Matthieu Nov 20 '17 at 20:29
  • As for compile problems, well, just read what the compiler says ;) (try to name your parameters?) – Matthieu Nov 20 '17 at 20:31