samedi 27 avril 2019

How can we use varargs for objects and compare their instance variables in the method

Suppose i have a Person class with some instance variables like age i want to make a varargs method that will return me a younger one object of person I'm choosing varargs because by the span of time the number of objects will be less or more, at time 0 there will be only 1 object, as time increases the number of objects will be more.

This method will give me the younger one and the younger will be removed, these objects will be stored in the arraylist before passing to varargs method

Person p1 = new Person();
Person p2 = new Person();

public Person youngerOne(Person ... p){

//Here i want to compare but I'm not sure how can i do this with varargs
//I.e if(p1.getAge() < p2.getAge()){
Return p1;
}Else{
P2}

}

Aucun commentaire:

Enregistrer un commentaire