我实现了一个地址簿和一个地址类!我想实现一个方法“searchForename”和“searchSurename”,这是我之前用“insert”添加的搜索元素。我查看了使用 ArrayList 搜索对象或元素的其他实现,但大多数情况下它们真的让我感到困惑,因为太多的代码没有任何解释。
第三次编辑新问题!:
第三次尝试:
import java.util.ArrayList;
public class Adressbook {
private ArrayList<Adress> adresses = null;
public Adressbook(){
adresses = new ArrayList<Adress>();
}
public void insert(Adress adress){
adresses.add(new Adress("Marvin","Wildman","Blastreet",9,12345,"Blatown"));
adresses.add(new Adress("Anne","Wildman","Woodstreet",10,6789,"Exampletown"));
adresses.add(new Adress("William","Wildman","Eaglestreet",11,73975,"Blubvalley"));
}
public void searchSurename(String surename){
for(Adress s: adresses){
if("Green".equals(surename)){
System.out.println(s);
}
}
}
public void searchForename(String forename){
for(Adress s: adresses){
if("Anne".equals(forename)){
System.out.println(s);
}
}
}
public String toString(){
return null;
}
}
我有几个问题:
1.Adressbook中的toString方法是怎样的?
2.Adress 类的 toString 是什么样子的?
3.Adress 类中的构造函数看起来正确吗?
4.我可以比这更容易/更有效地实现搜索方法吗?如果不正确,我该如何更改?
忘记我的班级地址:
public class Adress {
public static String forename;
public static String surename;
public static String street;
public static int houseno;
public static int code;
public static String state;
public Adress(String forename, String surename, String street, int houseno, int code,String state){
this.forename = forename;
this.surename = surename;
this.street = street;
this.houseno = houseno;
this.code = code;
this.state = state;
}
public String toString(){
return null;
}
}