有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

继承获取父子类java的不兼容错误

我定义了一个HashMap:

        HashMap<String,ArrayList<Thing>> hashmap = new HashMap<>();

我确实需要一个ArrayList,因为我希望一个键有多个值。这是我为那个问题找到的最佳解决方案。 Thing类是其他一些类的父类。问题是,当我尝试添加时,会出现错误

 hashmap.put(b, world.ports);

这就是错误:

no suitable method found for put(String,ArrayList<SeaPort>)
method Map.put(String,ArrayList<Thing>) is not applicable
  (argument mismatch; ArrayList<SeaPort> cannot be converted to ArrayList<Thing>)
method AbstractMap.put(String,ArrayList<Thing>) is not applicable
  (argument mismatch; ArrayList<SeaPort> cannot be converted to ArrayList<Thing>)
method HashMap.put(String,ArrayList<Thing>) is not applicable
  (argument mismatch; ArrayList<SeaPort> cannot be converted to ArrayList<Thing>)

` 我不明白这一点,因为海港延伸的东西不应该是兼容的吗?我读过一些向上和向下的帖子,但我不知道它们在这里是如何应用的。 下面是课堂上的事情:

package seaportprogram;

import java.util.Scanner;


public class Thing implements Comparable<Thing>{
String name;
int index;
int parent;
World world;

public Thing(){
    name = null;
    index = 0;
    parent =0;
}

//Thing Scanner Constructor
public Thing (Scanner sc){
if(sc != null){
name = sc.next();
index = sc.nextInt();
parent = sc.nextInt();
    }
}

// Get Index
public int getIndex(){
    return index;
}
//Get Name
public String getName(){
    return name;
}
//Get Parent
public int getParent(){
return parent;
}
//Thing To String
public String toString(){
return name + " " + index;
}
//Auto-Gen Compare Method
@Override
public int compareTo(Thing o) {
    // TODO Auto-generated method stub
    return 0;

}

}//End - Class Thing

这是海港级别:

package seaportprogram;

import java.util.ArrayList;
import java.util.Scanner;

class SeaPort extends Thing {
ArrayList<Dock> docks;
ArrayList<Ship> que;
ArrayList<Ship> ships;
ArrayList<Person> persons;

//Sea Port Scanner Constructor
public SeaPort(Scanner sc){

    super(sc);
    docks = new ArrayList<>();
que = new ArrayList<>();
ships = new ArrayList<>();
persons = new ArrayList<>();
}
//Set Docks
public void setDocks(ArrayList<Dock> docks){
    this.docks = docks;
}
//Get Docks
public ArrayList<Dock> getDocks(){
    return docks;
}
//Set Ships
public void setShips(ArrayList<Ship> ships){
    this.ships = ships;
}
//Get Ships
public ArrayList<Ship> getShips(){
    return ships;
}
//Set Que
public void setQue(ArrayList<Ship> que){
    this.que = que;
}
//Get Que
public ArrayList<Ship> getQue(){
    return que;
}
//Sea Port To String
public String toString(){

String string = "\n\n Sea Port: " + super.toString() + "\n";

for(Dock md: docks){
        string += "\n" + md + "\n";
}

string += "\n\n --- List of all ships in Que: ";

for(Ship ms: que){
        string += "\n  > " + ms;
}

string += "\n\n --- List of all Ships:";

for(Ship ms: ships){
        string += "\n  > " + ms;
    }

string += "\n\n --- List of all Persons:";

for(Person mp: persons){
        string += "\n  > " + mp;
}
return string;

}//End 


}// End Sea Port Class

这是世界级的:

package seaportprogram;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;
public class World extends Thing{

ArrayList<SeaPort> ports;
PortTime time;

//World Scanner Constructor
public World(Scanner sc){
    super (sc);
ports = new ArrayList<>();
}
//Set Ports
}

有什么办法可以做到吗? 蒂娅! 更新: 我正试图打印地图,但我得到了一个空指针异常。我认为这是因为列表在seaport和MyHashMap类中都被初始化了。我似乎找不到解决这个问题的方法,我甚至不确定这是错误的原因。下面是toString()

          public String toString(){
        if(map.isEmpty())
         {
             System.out.println("The hashMap is empty");
             return "empty";
         }
        String display=" ";
        Iterator<String> itr = map.keySet().iterator();
        while (itr.hasNext()) {
            display =display + itr.next();
            Iterator<ArrayList<T>> itr2 = map.values().iterator();
            while (itr2.hasNext()) {
                display +=itr2.next();
            }
        }

    return display;
    }

这是gui对它的调用:

 jta.setText(map.toString());

共 (1) 个答案

  1. # 1 楼答案

    Seaport可以扩展Thing,但是ArrayList<SeaPort>不扩展ArrayList<Thing>

    如果只想存储字符串到对象的映射,则不需要ArrayList。如果你真的想把字符串映射到事物列表,那么你不能这样做