1

嘿,我正在尝试为飞机创建一个 hashMap 存储。但是当我添加时,它只会打印出一个航班。谁能帮我这个。这是我的代码:

import java.util.Scanner;


public class MainApp 
{
    private Scanner keyboard = new Scanner(System.in);
    public static void main(String[] args)
    {
        new MainApp().start();  
    }

    public void start()
    {
        Airline aerLingus = new Airline("AerLingus");
        PlaneStore planeStore = new PlaneStore("Aer Lingus");

        Flight p1 = new Flight("Aer Lingus","A01", 150.5, 10.5, 500, Flight.AIRPLANETYPE.AIRBUS);
        Flight p2 = new Flight("Aer Lingus","B01", 50.3, 1.5, 91, Flight.AIRPLANETYPE.CORPORATE);
        Flight p3 = new Flight("Aer Lingus","C01", 12.2, -3.1, 56, Flight.AIRPLANETYPE.AIRBUS);


        Flight p4 = new Flight("Ryan Air","D01", 10.5, 1.5, 430, Flight.AIRPLANETYPE.PRIVATE);
        Flight p5 = new Flight("Ryan Air","E01", 0.3, 2.1, 101, Flight.AIRPLANETYPE.CORPORATE);
        Flight p6 = new Flight("Ryan Air","F01", 2.2, -3, 291, Flight.AIRPLANETYPE.AIRBUS);
        planeStore.addFlight("",p1);
        planeStore.addFlight("",p2);
        planeStore.addFlight("",p3);
        planeStore.print();

        aerLingus.add(planeStore);
        aerLingus.add(planeStore);
        aerLingus.add(planeStore);

        aerLingus.printPlane();
    }

}

import java.util.TreeMap;


public class PlaneStore 
{
    private String airlineName;
    private TreeMap<String,Flight> planeMap;


    public PlaneStore(String airlineName) 
    {
        this.airlineName = "";
        planeMap = new TreeMap<String,Flight>();
    }


    public String getAirlineName() {
        return airlineName;
    }


    public TreeMap<String, Flight> getFlightList() {
        return planeMap;
    }
    public void addFlight(String airline,Flight flight)
    {
        planeMap.put(airline, flight);
    }

    // ---------------------------------------------------------------------------------------
    // Name: Print.
    // ---------------------------------------------------------------------------------------
    public void print()
    {
        System.out.println("\n********Student's in the Company.********");
        for (Flight flight : planeMap.values()) {
            // System.out.println(employee); to print the toString of Employee
            // class
            // or:
            System.out.println("Airline:\t" + flight.getAirline());
            System.out.println("Flight Number:\t" + flight.getFlightNumber());
            System.out.println("Fuel Remaining:\t" + flight.getFuelRemaining());
        }

    }


}
public class Flight 
{
    private String airline;
    private String flightNumber;
    private double fuelRemaining;
    private double overdue;
    private int passengerNumber;
    private AIRPLANETYPE planeType;

    public enum AIRPLANETYPE
    {
        AIRBUS("1"), CORPORATE("2"), PRIVATE("3");

        private String planeName;

        private AIRPLANETYPE(String planeName)
        {
            this.planeName = planeName;
        }
    }


    public Flight(String airline, String flightNumber, double fuelRemaining,
            double overdue, int passengerNumber, AIRPLANETYPE planeType) 
    {
        super();
        this.airline = airline;
        this.flightNumber = flightNumber;
        this.fuelRemaining = fuelRemaining;
        this.overdue = overdue;
        this.passengerNumber = passengerNumber;
        this.planeType = planeType;
    }

    public String getAirline() {
        return airline;
    }

    public void setAirline(String airline) {
        this.airline = airline;
    }

    public String getFlightNumber() {
        return flightNumber;
    }

    public void setFlightNumber(String flightNumber) {
        this.flightNumber = flightNumber;
    }

    public double getFuelRemaining() {
        return fuelRemaining;
    }

    public void setFuelRemaining(double fuelRemaining) {
        this.fuelRemaining = fuelRemaining;
    }

    public double getOverdue() {
        return overdue;
    }

    public void setOverdue(double overdue) {
        this.overdue = overdue;
    }

    public int getPassengerNumber() {
        return passengerNumber;
    }

    public void setPassengerNumber(int passengerNumber) {
        this.passengerNumber = passengerNumber;
    }

    public AIRPLANETYPE getPlaneType() {
        return planeType;
    }

    public void setPlaneType(AIRPLANETYPE planeType) {
        this.planeType = planeType;
    }


    public String toString() 
    {
        return "Flight [airline=" + airline + ", flightNumber=" + flightNumber
                + ", fuelRemaining=" + fuelRemaining + ", overdue=" + overdue
                + ", passengerNumber=" + passengerNumber + ", planeType="
                + planeType + "]";
    }




}
4

3 回答 3

7

HashMap 是一种数据结构,您可以在其中存储键值对。密钥必须是唯一的。否则,您将覆盖具有相同键的值。

使用这种数据结构,您可以非常快速地添加和查找值,因为添加和查找值的复杂性是恒定的。但缺点是您只能添加一次密钥。

于 2012-12-10T19:56:42.483 回答
0

编辑以响应进一步的代码发布:

每次您这样做时,您都会PlaneStore.addPlane()提供一个空白键。Hashmap 仅在您为每个对象提供唯一键时才有用。否则每个平面都会覆盖前一个平面。这就是为什么当您打印时您只会看到一个对象。给每个飞机一个唯一的键,可能是一个尾号或相关的东西,你应该能够检索每个对象。

于 2012-12-10T19:44:22.277 回答
-1

您需要按如下方式定义您的哈希图:

Map<String, List<Flight>> map = new TreeMap<String, List<Flight>>();

然后对于每个航班名称,您需要创建一个新的航班列表,如下所示:

map.get("foo") = new ArrayList<Flight>();

map.get("foo").add(Flight);

对于上面的代码,你需要防御,如果没有列表,你需要创建一个,否则你会得到一些异常。此外,确保每个飞行键只创建一个列表。

于 2012-12-10T19:42:51.867 回答