I know that the subject exists on StackOverFlow, but I don't understand really the logic. Comparing two class objects with a method
I have a class Player
with 3 variables:
...
public String name;
public int age;
public boolean sex;
public Player(String name, int age, boolean sex){
this.name = name;
this.age = age;
this.sex = sex;
}
public void display(int number){
System.out.println("-----------------------");
System.out.println("Number : " + number);
System.out.println("Name : " + name);
System.out.println("Age : " + age);
System.out.println("Sex : " + sex);
}
...
Then, I create 2 objects into my file Main
as below:
List<Player> players = new ArrayList <Player>();
players.add(new Player("Eric", 31, true));
players.add(new Player("Juliette", 27, false));
I would like to compare the variable age
from the object1 to the object2 and after, I have to display the name of the Player who is the oldest player. So here, it's Eric
.
I am stuck into my method displayOldestAge()
I don't understand how to compare 2 ages by adding the name of the player.
public void displayOldestAge(int[] arrayPlayer){
int oldestAge = 0;
for(int i=0; i<arrayPlayer.length; i++){
if(arrayPlayer[i].age > oldestAge.age){
oldestAge = arrayPlayer[i];
}
}
System.out.println("The player oldest age is " + ???);
}