如何实现PersonRegister类的nameOftheOldest()方法获取最年长者姓名
扩展PersonRegister类实现查找最年长者姓名
任务说明
扩展PersonRegister类,编写nameOftheOldest()方法,该方法需从allPersons数组中找出并返回注册列表中年龄最大的人的姓名。
现有代码
PersonRegister类代码
public class PersonRegister { // instance variables - replace the example below with your own private Person[] allPersons; /** * Constructor for objects of class PersonRegister */ public PersonRegister(int numberOfPersons) { // initialise instance variables this.allPersons = new Person[numberOfPersons]; } public void registreraPerson(int index, Person aPerson) { allPersons[index] = aPerson; } }
Person类代码
public class Person { // instance variables - replace the example below with your own private String name; private int birthYear; /** * Constructor for objects of class ExperimentJavaLoops */ public Person(String name, int birthYear) { // initialise instance variables this.name = name; this.birthYear = birthYear; } public String getName() { return name; } public int birthYear() { return birthYear; } public void setName(String name) { this.name = name; } public void setbirthYear(int birthYear) { this.birthYear = birthYear; } }
我的困惑
我查了很多资料、读了不少文献也试着写了代码,但还是搞不定这个方法。我知道要遍历数组,用循环加if语句比较值,还要调用Person类的方法获取姓名和出生年份(毕竟年龄最大的人出生年份最小),而且得返回字符串类型的姓名。但我是编程新手,平时更熟悉ArrayList,对数组操作有点懵,希望能得到帮助。
内容的提问来源于stack exchange,提问作者Hexatronic
相关产品推荐
相关产品推荐

