-2
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 airline1 = 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.add(p1);
        planeStore.add(p2);
        planeStore.add(p3);
        planeStore.print();

        airline1.add(planeStore);
        airline1.add(planeStore);
        airline1.add(planeStore);

        airline1.printPlane();
    }

}

import java.util.HashMap;


public class Airline 
{
    private String airlineName;
    private HashMap<String, PlaneStore> map;

    public Airline(String airlineName)
    {
        this.airlineName = "";
        map = new HashMap<String, PlaneStore>();
    }
    public void add(PlaneStore plane)
    {
        map.put(airlineName, plane);
    }
    public void remove(String flight)
    {
        map.remove(flight);
    }
    public void printPlane()
    {
        System.out.println("\n********Flight List********");
        for (PlaneStore plane: map.values()) {
             //System.out.println(plane);
            // class
            // or:
            System.out.println(airlineName);
            System.out.println(plane.toString());

        }

    }

}

嘿,我想打印出飞机商店。平面存储包含字符串和航班的哈希图。这就是 flight.add/print 进入主应用程序的地方。但我现在希望航空公司和航班并行打印,所以我放了一个航空公司商店,我试图把飞机商店和航空公司一起打印出来,但我得到的只是一个内存地址,任何人都可以帮我解决这个问题。

4

4 回答 4

1

如果你得到类似的东西MyClass@abcdef,那么你需要toStringMyClass这样覆盖:

public class MyClass {

    ...

    @Override
    public String toString() {
        return "My class"; // Make a string here that you want to display
    }
}

因此,例如,如果PlaneStorehasint planeCountString namefor 字段,那么您可以执行以下操作:

@Override
public String toString() {
    return "Plane store | Name: " + name + ", planes: " + planeCount;
}
于 2012-12-10T20:23:11.680 回答
0

因为您似乎是编程新手,所以我将交给您他的答案:

public class PlaneStore {

    private String name;
    ...

    public String toString() {
        return name;
    }
}
于 2012-12-10T20:29:03.640 回答
0

尝试使用 Apache ToStringBuilder 类。但当然 toString() 也会有所帮助。关于ToStringBuilder的更多信息

于 2012-12-10T20:20:54.073 回答
0

你可以这样做:

System.out.println(map.toString());

这与

System.out.println(map);

然后,为了避免您提到的“内存地址”:
向 PlainStore 添加一个方法

public String toString() {
    return this.storeName; // or similar, 
}

这是因为 Map 可以通过调用toString()每个键和值的方法打印出它的内容。

如果此输出不够好,您必须自己进行迭代:使用map.entrySet()并对其进行迭代。

于 2012-12-10T20:16:28.963 回答