【问题标题】:How do I search through an ArrayList for the lowest number and shortest duration? [closed]如何在 ArrayList 中搜索最少数量和最短持续时间? [关闭]
【发布时间】:2013-03-19 22:56:30
【问题描述】:

我有 3 节课。我正在学习接口和接口类 TravelCost 必须具有公共抽象以及方法类型和名称,以便它在所有三个类中都是一致的。这三个类(AirTravelCost、TrainTravelCost、CarTravelCost)将实现 TravelCost。我已经完成了所有设置并经过测试可以正常工作。但是,我假设的测试页面是您通过 arrayList 输入搜索以获取最低成本和最短持续时间的地方。我不知道该怎么做,因为我以前从未在 ArrayList 中这样做过。这里是测试类中的示例代码:

import java.util.*;

 public class TestTravelCost
 {
   public static void main(String [] args)
   {
    /*Scanner scn = new Scanner(System.in); //scanner object

    System.out.println("Number of Miles: ");
    double numOfMiles = scn.nextDouble();

    System.out.println("Hotel cost per night: ");
    double cost = scn.nextDouble();

    System.out.println("Description: ");
    String description = scn.nextLine();*/

    TravelCost c = new CarTravelCost(400, 200, "Boston");//instantiate object for car travel
    TravelCost t = new TrainTravelCost(6, 60.0, "Boston"); //instantiate object for train travel
    TravelCost a = new AirTravelCost(224, "20110103", "0743" , "20110103", "1153", "Boston");//instantiate object for air travel

    ArrayList<TravelCost> AL = new ArrayList<TravelCost>();//array list for car travel
    AL.add(c);
    AL.add(t);
    AL.add(a);

    for(TravelCost tc : AL)
    {
        System.out.println(tc.toString());
    }
   }
}

输出: 开车到波士顿需要 7.2727272727272725 小时,费用 210.0
前往波士顿的火车旅行需要 6.0 小时,费用为 70.0
飞往波士顿的航空旅行将花费 1.0166666666666666 和成本 243.48888888888888 //这不是正确的计算,我不知道我错在哪里,但它假设与最短持续时间相同。我想我不擅长数学.

这是我用于航空旅行的计算方法

    public double getDuration()
{
    //---DEPARTURE---//
    int Dyear = Integer.parseInt(departureDate.substring(0,3)); //2011
    int Dmonth = Integer.parseInt(departureDate.substring(4,5));//01
    int Dday = Integer.parseInt(departureDate.substring(6,7));//03

    int Dhour = Integer.parseInt(departureTime.substring(0,1));//0743
    int Dminute = Integer.parseInt(departureTime.substring(2,3));//1153
    //---ARRIVAL---//
    int Ayear = Integer.parseInt(arrivalDate.substring(0,3)); //2011
    int Amonth = Integer.parseInt(arrivalDate.substring(4,5));//01
    int Aday = Integer.parseInt(arrivalDate.substring(6,7));//03

    int Ahour = Integer.parseInt(arrivalTime.substring(0,1));//0743
    int Aminute = Integer.parseInt(arrivalTime.substring(2,3));//1153

    GregorianCalendar date = new GregorianCalendar(Dyear, Dmonth, Dday, Dhour, Dminute);//departure date & time
    GregorianCalendar time = new GregorianCalendar(Ayear, Amonth, Aday, Ahour, Aminute);//arrival date & time

    //date = arrivalDate - departureDate;//2011-01-03 - 2011-01-03 = 0
    //time = arrivalTime - departureTime;//0734 - 1153 = 410

    double duration = (Math.abs(date.getTimeInMillis() - time.getTimeInMillis()) / 60000.0) / 60.0;
    return duration;
  `enter code here` }

如何在我的代码中得到这个结果?

最低费用:前往波士顿的火车旅行需要 11.0 小时,费用为 70.0
最短持续时间:飞往波士顿的航空旅行需要 4.166666666666667 小时,费用为 234.0

【问题讨论】:

    标签: java arrays arraylist foreach max


    【解决方案1】:

    您没有显示TravelCost 界面,但要实现您想要的,它至少应该有一个 getDuration 和 getCost 方法。

    public interface TravelCost {
        ... // what you already have in the interface definition
        public double getDuration();
        public double getCost();
    }
    

    有了这些,我会创建一个小的虚拟类来实现这些属性的可比性:

    public DummyTC implements TravelCost {
        private double cost;
        private double duration;
    
        public DummyTC(double cost, double duration) {
            this.cost = cost;
            this.duration = duration;
        }
    
        public double getDuration() {
            return duration;
        }
    
        public double getCost() {
            return cost;
        }
    
        // and other methods/properties imposed by TravelCost
    }
    

    这将使您能够找到您要查找的内容:

    // instantiate 2 DummyTC's with impossibly high cost &durations
    
    TravelCost cheapest = new DummyTC(99999999999.99, 99999999999.99);
    TravelCost shortest = new DummyTC(99999999999.99, 99999999999.99);
    
    // iterate over the List
    
    for(TravelCost tc : AL) {
        // if present tc is cheaper than cheapest, swap
        if ( tc.getCost() < cheapest.getCost() ) {
            cheapest = tc;
        }
        // if present tc is shorter than shortest, swap
        if ( tc.getDuration() < shortest.getDuration() ) {
            shortest = tc;
        }
    }
    
    // at this point cheapest and shortest will contain the cheapest and shortest 
    // ways of transportation, so we print them out.
    
    System.out.println(cheapest.toString());
    System.out.println(shortest.toString());
    

    另外,您的日期处理代码非常复杂。看看SimpleDateFormat

    Date date = null;
    Date time = null;
    // create a SimpleDateFormat instance for your time/date format
    SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
    try {
        // parse it
        date = format.parse(departureDate);
        // done
    } catch (ParseException e) {
        // departureDate could not be parsed, you should handle that case here
    }
    
    try {
        // parse it
        time = format.parse(arrivalTime);
        // done
    } catch (ParseException e) {
        // arrivalTime could not be parsed, you should handle that case here
    }
    

    由于 Date 也有 routine to get the epoch-millis,您可以继续使用已有的代码,尽管在这种情况下,long 可能是比双精度更好的返回类型。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-10-09
      • 2013-10-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多