Java实现从Person数组中查找最年长者的方法求助
oldest Method for Your Person Class Hey there! Let's walk through how to build that oldest method you need. I'll keep it straightforward since you mentioned trying basic for loops already—we'll stick with that approach to keep things familiar.
First, Let's Clarify the Goal
The "oldest" person in your array is the one with the earliest year of birth (since a smaller birth year means they were born earlier, hence older than someone born later). We can compare birth years directly instead of calculating age (though using getAge() would also work—this is just more efficient, especially for historical figures like your sample data).
Step-by-Step Implementation
Here's how to fill in that empty oldest method, with explanations for each part:
public String oldest(Person[] persons) { // 1. Handle edge cases first to avoid runtime errors if (persons == null || persons.length == 0) { return "No valid person data provided."; } // 2. Start with the first person as our initial "oldest" candidate Person oldestPerson = persons[0]; // 3. Loop through the rest of the array to find someone older for (int i = 1; i < persons.length; i++) { Person currentPerson = persons[i]; // Compare birth years: smaller value = earlier birth = older person if (currentPerson.yearOfBirth < oldestPerson.yearOfBirth) { oldestPerson = currentPerson; } } // 4. Return the oldest person's string using your existing toString() method return oldestPerson.toString(); }
Let's Break It Down
- Edge Case Handling: We check if the input array is
nullor empty first—this prevents crashes and gives a clear message when there's no data to process. - Initial Candidate: We start with the first person in the array as our baseline for "oldest."
- Loop Through the Array: Starting from index 1 (since we already used index 0), we compare each person's birth year to our current oldest. If someone was born earlier, they become the new oldest candidate.
- Return Result: We use your existing
toString()method to return the oldest person's name and birth year as a string, which matches the method's required return type.
Testing with Your Sample Input
If you pass in your sample Person array:
Person[] people = { new Person("Augusta Ada King, grevinna av Lovelace", 1815), new Person("Muhammad ibn Musa al-Khwarizmi", 780), new Person("Alan Turing", 1912), new Person("Grace Hopper", 1906) };
Calling oldest(people) will return:
Muhammad ibn Musa al-Khwarizmi 780
Which is correct, since he's the earliest-born person in your list.
Alternative: Enhanced For Loop (If You Prefer)
If you want a cleaner iteration without using an index, here's an enhanced for loop version that works the same way:
public String oldest(Person[] persons) { if (persons == null || persons.length == 0) { return "No valid person data provided."; } Person oldestPerson = persons[0]; for (Person person : persons) { if (person.yearOfBirth < oldestPerson.yearOfBirth) { oldestPerson = person; } } return oldestPerson.toString(); }
内容的提问来源于stack exchange,提问作者LoveFile

