我制作了一个包含三个类的联系人列表,ContactList 类包含一个存储姓氏、名字、街道、城市、州、邮编、国家、电子邮件、电话号码和便笺的数组。
我想在 ContactList 类中实现一个按姓氏搜索的功能,它显示了具有用户搜索的姓氏的联系人的所有信息,但我似乎无法工作。:(
import java.util.*;
public class ContactList {
//declaration of an array and its attributes
private final int SIZE = 10;
private Person [ ] list;
private int nextEmptyElementInArray = 0;
// Constructor for ContactList object
public ContactList () {
list = new Person [SIZE];
}
// Method that adds a new contact into the array
public void addNewContact() {
list[nextEmptyElementInArray] = new Person();
list[nextEmptyElementInArray].read();
nextEmptyElementInArray++;
}
// Method retrieves contacts by last name
int searchByLastName(Person [] list) {
Scanner console;
console = new Scanner(System.in);
String searchByLastName = console.next();
for (int i= 0; i< list.length; i++) {
if (list[nextEmptyElementInArray].lastName.equals(searchByLastName))
return i;
}
return -1;
}
}