Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用getValue从HashMap中获取多个“一”值_Java_Hashmap_Getvalue - Fatal编程技术网

Java 如何使用getValue从HashMap中获取多个“一”值

Java 如何使用getValue从HashMap中获取多个“一”值,java,hashmap,getvalue,Java,Hashmap,Getvalue,我正在用JAVA编写一个程序,我正在使用HashMap private HashMap<Integer,Plane> planes; 然后,我尝试打印HashMap的所有组件,如下所示: public void getAllAircrafts () { Set set = planes.entrySet(); Iterator iterator = set.iterator(); while(iterator.hasNext()) { Ma

我正在用JAVA编写一个程序,我正在使用HashMap

 private HashMap<Integer,Plane> planes;
然后,我尝试打印HashMap的所有组件,如下所示:

public void getAllAircrafts ()
{
    Set set = planes.entrySet();
    Iterator iterator = set.iterator();
    while(iterator.hasNext()) {
        Map.Entry mentry = (Map.Entry)iterator.next();
        System.out.print("Aircraft ID is: "+ mentry.getKey() + " ");
        System.out.println(mentry.getValue());
    }
}

问题是我想打印所有描述飞机的变量的值,但我得到的是飞机。Plane@15db9742来自mentry.getValue()。如何解决此问题?

您需要为平面类重写并添加toString方法

    @Override
    public String toString() {
        return "Plane [planeNumber=" + planeNumber + 
                       ",departureTime=" + departureTime +  
                       ",arrivalTime=" + arrivalTime + 
                       ",flightDuration=" + flightDuration + 
                       ",aerialDrops=" + aerialDrops + "]";
    }

现在您正在使用对象类父对象的toString方法

您需要为平面类重写并添加一个toString方法

    @Override
    public String toString() {
        return "Plane [planeNumber=" + planeNumber + 
                       ",departureTime=" + departureTime +  
                       ",arrivalTime=" + arrivalTime + 
                       ",flightDuration=" + flightDuration + 
                       ",aerialDrops=" + aerialDrops + "]";
    }

现在您正在使用对象类父对象的toString方法,根据需要稍作更改即可:

   public class Plane {
   private int planeNumber;
   private int planeNumber;
   private int arrivalTime;
   private int flightDuration;
   private int aerialDrops;

   public String toString()
{
  return planeNumber +" "+planeNumber+" "+arrivalTime+" "+flightDuration+" "+aerialDrops
}
}

根据您的需要,稍作改动即可:

   public class Plane {
   private int planeNumber;
   private int planeNumber;
   private int arrivalTime;
   private int flightDuration;
   private int aerialDrops;

   public String toString()
{
  return planeNumber +" "+planeNumber+" "+arrivalTime+" "+flightDuration+" "+aerialDrops
}
}