传入并返回一个数组

时间:2015-09-24 20:15:10

标签: java

我有一个名为MarathonRunner的类和一个用于测试它的类MarathonRunnerTest

在我的测试类的main方法中,我创建了一个数组调用runners,其中包含15个人对象(每个对象都有一个名字,姓氏,地址和运行马拉松的时间) 。然后,我想将该数组传递给我的MarathonRunnergetFastTestRunner()中定义的方法,该类采用类型为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;
}

3 个答案:

答案 0 :(得分:2)

我建议使用Java Collections API,并在BanffMarathonRunner中实现Comparable接口。这简单地要求您实现compareTo()函数:

public int compareTo(BanffMarathonRunner otherRunner) {
    return Integer.compare(this.getTime(), otherRunner.getTime());
}

在您的通话代码中,将选手放入List,然后只需拨打Collections.sort()即可。由于您已经开始使用array[]投手,因此您可以使用Arrays.asList(runners)

如果您对在跑步者中设置compareTo()功能感到不满意(因为嘿,您可能希望稍后按姓氏对其进行排序),您可以随时使用sort(List<T> list, Comparator<? super T> c),坚持你想要的任何排序行为。在您的情况下,第二个参数将是:

new Comparator<BanffMarathonRunner >() {
  public int compare(BanffMarathonRunner a, BanffMarathonRunner b) {
    return a.getTime().compareTo(b.getTime());
  }});

我会告诫说,就像其他人所说的那样,你不一定需要对清单进行排序,因为你只是想找到最短的时间。

答案 1 :(得分:1)

来自其他答案的比较器+排序解决方案将为您提供使所有跑步者按顺序排列的额外好处。

如果你只关心最快的那个,你只需要遍历数组并保持迄今为止发现的最快的跑步者。最后,你将拥有整个阵列中最快的跑步者。

public static BanffMarathonRunner getFastestRunner(BanffMarathonRunner[] runners){

    BanffMarathonRunner currentFastest = runners[0];

    for (BanffMarathonRunner runner : runners){
        if (runner.getTime() < currentFastest.getTime()) {
            currentFastest = runner;
        }
    }

    return currentFastest;
}

然后你就叫它:

BanffMarathonRunner fastestRunner = getFastestRunner(runners);

在旁注 - 如果您要拨打System.out.println(fastestRunner);,您应该覆盖toString()课程中的BanffMarathonRunner方法,以指定每个参赛者应准确打印的内容。

答案 2 :(得分:1)

你应该实现Comparable接口并对数组进行排序(使用Arrays.sort。最快的跑步者是排序数组中的第一个对象。

但是你应该考虑多个跑步者拥有相同最快时间的情况。因此,您应该返回一个包含相同时间(getFastestRunners()

的第一个项目的数组
相关问题