Java 传入并返回数组
我有一个叫做Java 传入并返回数组,java,Java,我有一个叫做MarathonRunner的类,还有一个用来测试它的类叫做marathonrunertest 在我的测试类的main方法中,我创建了一个数组调用runners,其中包含15个person对象(每个对象都有一个名称、姓氏、地址以及他们跑马拉松所花费的时间)。然后,我想将该数组传递到在我的MarathonRunner类中定义的方法中,该类名为getFastTestRunner(),它采用MarathonRunner类型的数组。然后,该方法应该返回在最短时间内跑完马拉松的跑步者的姓名和地
MarathonRunner
的类,还有一个用来测试它的类叫做marathonrunertest
在我的测试类的main方法中,我创建了一个数组调用runners
,其中包含15个person对象(每个对象都有一个名称、姓氏、地址以及他们跑马拉松所花费的时间)。然后,我想将该数组传递到在我的MarathonRunner
类中定义的方法中,该类名为getFastTestRunner()
,它采用MarathonRunner
类型的数组。然后,该方法应该返回在最短时间内跑完马拉松的跑步者的姓名和地址
我真的被困在这个问题上,不知道该往哪个方向走,也不知道如何实施
我应该尝试对数组进行排序,然后将其全部传回,还是使用该方法查找最快的跑步者并传回其中只有最快跑步者的数组对象
我的问题是双重的。我不确定我应该使用哪种策略以及如何实施它。我会给你我目前所得到的
public class BanffMaratonRunnerTest{
public static void main(String[] args) {
BanffMarathonRunner elenaBrandon = new BanffMarathonRunner("Elena", "Brandon", "123 Street", 341, 1);
BanffMarathonRunner thomasMolson = new BanffMarathonRunner("Thomas", "Molson", "Box 2222", 273, 2);
BanffMarathonRunner hamiltonWinn = new BanffMarathonRunner("Hamilton", "123 Street", "Winn", 278, 5);
BanffMarathonRunner suzieSarandin = new BanffMarathonRunner("Suzie", "123 Street", "Sarandin", 329, 7);
BanffMarathonRunner philipWinne = new BanffMarathonRunner("Philip", "Winne", "Box 2222", 445, 9);
BanffMarathonRunner alexTrebok = new BanffMarathonRunner("Alex", "123 Street", "Trebok", 275, 3);
BanffMarathonRunner emmaPivoto = new BanffMarathonRunner("Emma", "123 Street", "Pivoto", 275, 4);
BanffMarathonRunner johnLenthen = new BanffMarathonRunner("John", "Lenthen", "Box 2222", 243, 1);
BanffMarathonRunner jamesLean = new BanffMarathonRunner("James", "123 Street", "Lean", 334, 1);
BanffMarathonRunner janeOstin = new BanffMarathonRunner("Jane", "Ostin", "Box 2222", 412, 1);
BanffMarathonRunner emilyCar = new BanffMarathonRunner("Emily", "Car", "Box 2222", 393, 4);
BanffMarathonRunner[] runners = {elenaBrandon,thomasMolson,hamiltonWinn,suzieSarandin,philipWinne,
alexTrebok,emmaPivoto,johnLenthen,jamesLean,janeOstin,emilyCar};
BanffMarathonRunner[] fastestRunner = BanffMarathonRunner.getFastestRunner(runners[0]);
System.out.println(fastestRunner[0]);
}
public static BanffMarathonRunner getFastestRunner(BanffMarathonRunner[] runners){
int fastest;
int currentFastest = runners[0].getTime();
int nextTest;
for (int i = 1; i <= 15; i++){
if (currentFastest == runners[2].getTime());{
// just trying to get it to return the third runner no matter for testing
BanffMarathonRunner fastestRunner = runners[i];
return fastestRunner;
}
}
return null;
}
公共类BanffMaratonRunnerTest{
公共静态void main(字符串[]args){
BanffMarathonRunner elenaBrandon=新的BanffMarathonRunner(“Elena”,“Brandon”,“123街”,341,1);
班夫马拉松长跑者汤马斯莫尔森=新班夫马拉松长跑者(“托马斯”、“莫尔森”、“2222号信箱”、273、2);
班夫马拉松长跑者哈密尔顿酒店=新班夫马拉松长跑者(“汉密尔顿”,“123街”,“温恩”,278,5);
班夫马拉松长跑者suzieSarandin=新班夫马拉松长跑者(“Suzie”,“123街”,“Sarandin”,329,7);
班夫马拉松长跑运动员菲利普温内=新班夫马拉松长跑运动员(“菲利普”、“温妮”、“2222号包厢”、445、9);
BanffMarathonRunner alexTrebok=新的BanffMarathonRunner(“Alex”,“123街”,“Trebok”,275,3);
班夫马拉松跑步者emmaPivoto=新班夫马拉松跑步者(“Emma”,“123街”,“Pivoto”,275,4);
班夫马拉松长跑运动员约翰伦森=新班夫马拉松长跑运动员(“约翰”、“伦森”、“2222号包厢”、243、1);
BanffMarathonRunner jamesLean=新的BanffMarathonRunner(“詹姆斯”,“123街”,“精益”,334,1);
BanffMarathonRunner Janostin=新的BanffMarathonRunner(“Jane”、“Ostin”、“2222号框”、412,1);
班芙马拉松跑步者艾米利卡=新班芙马拉松跑步者(“艾米丽”,“汽车”,“2222号信箱”,393,4);
班夫马拉松长跑者[]跑步者={elenaBrandon,thomasMolson,hamiltonWinn,suzieSarandin,philipWinne,
亚历克斯特雷博克、埃马皮托、约翰伦森、詹姆斯林、珍妮奥斯汀、埃米利卡};
BanffMarathonRunner[]fastestRunner=BanffMarathonRunner.getFastestRunner(runners[0]);
System.out.println(fastestRunner[0]);
}
公共静态BanffMarathonRunner getFastestRunner(BanffMarathonRunner[]跑步者){
int最快;
int currentfaster=runners[0]。getTime();
int NEXTEST;
对于(int i=1;i其他答案中的comparator+排序解决方案将为您带来额外的好处,让所有跑步者都井然有序
如果你只关心最快的一个,你只需要在数组中迭代,并保持目前为止找到的最快的跑步者。最终,你将拥有整个数组中最快的跑步者
public static BanffMarathonRunner getFastestRunner(BanffMarathonRunner[] runners){
BanffMarathonRunner currentFastest = runners[0];
for (BanffMarathonRunner runner : runners){
if (runner.getTime() < currentFastest.getTime()) {
currentFastest = runner;
}
}
return currentFastest;
}
另一方面,如果您想调用System.out.println(fastestRunner);
,您应该重写toString()BanffMarathonRunner
类中的
方法,指定每个跑步者应准确打印的内容。其他答案中的comparator+排序解决方案将为您带来额外的好处,让所有跑步者都井然有序
如果你只关心最快的一个,你只需要在数组中迭代,并保持目前为止找到的最快的跑步者。最终,你将拥有整个数组中最快的跑步者
public static BanffMarathonRunner getFastestRunner(BanffMarathonRunner[] runners){
BanffMarathonRunner currentFastest = runners[0];
for (BanffMarathonRunner runner : runners){
if (runner.getTime() < currentFastest.getTime()) {
currentFastest = runner;
}
}
return currentFastest;
}
另一方面,如果您想调用System.out.println(fastestRunner);
,您应该重写toString()BanffMarathonRunner
类中的
方法来指定每个跑步者应该打印的内容。我建议利用Java Collections API,并在BanffMarathonRunner
中实现接口。这只需要实现compareTo()
函数:
public int compareTo(BanffMarathonRunner otherRunner) {
return Integer.compare(this.getTime(), otherRunner.getTime());
}
在调用代码中,将跑步者放入列表
,然后只调用Collections.sort()
。由于您已经开始使用跑步者的数组[]
,因此可以使用数组.asList(跑步者)
如果您对在运行程序中设置compareTo()
函数不满意(因为您可能希望稍后按姓氏对它们进行排序),您可以始终使用并将任何排序行为粘贴到其中。在您的情况下,第二个参数是:
new Comparator<BanffMarathonRunner >() {
public int compare(BanffMarathonRunner a, BanffMarathonRunner b) {
return a.getTime().compareTo(b.getTime());
}});
新比较器(){
公共整数比较(BanffMarathonRunner a、BanffMarathonRunner b){
返回a.getTime().compareTo(b.getTime());
}});
我要警告的是,正如其他人所说,您不必对列表进行排序,因为您只需要查找最短的时间。我建议利用Java Collections API,并在BanffMarathonRunner
中实现接口。这只需要您实现compareTo()
函数:
public int compareTo(BanffMarathonRunner otherRunner) {
return Integer.compare(this.getTime(), otherRunner.getTime());
}
在调用代码中,将跑步者放入列表
,然后只调用Collections.sort()
。由于您已经开始使用跑步者的数组[]
,因此可以使用数组.asList(跑步者)
如果您对在运行程序中设置compareTo()
函数不满意(因为您可能希望稍后按姓氏对它们进行排序),您可以始终使用并将任何排序行为粘贴到其中。在您的情况下,第二个参数是:
new Comparator<BanffMarathonRunner >() {
public int compare(BanffMarathonRunner a, BanffMarathonRunner b) {
return a.getTime().compareTo(b.getTime());
}});
新比较器(){
公共整数比较(BanffMarathonRunner a、BanffMarathonRunner b){
返回a.getTime().compareTo(b.getTime());
}});
我要警告你,就像其他人说的,你不需要nec