Java HashMap作为字符串的ArrayList
首先,这是一个作业,所以我更多的是寻求帮助,然后是编码答案(不想作弊!)。我的任务是创建一个处理火车/火车站网络的程序。我所停留的部分添加电台及其连接,并将这些连接作为字符串数组列表返回。我已经在下面列出了到目前为止的代码,以及作业的摘录(与我现在所讲的部分相关)。我整个周末都在为这件事苦苦挣扎,所以任何帮助都将不胜感激 这只是我需要编辑的接口的实现,“MyNetwork”类。我只是觉得我一直在兜圈子,甚至可能还没有走上正轨 作业中的强>Java HashMap作为字符串的ArrayList,java,arrays,arraylist,hashmap,keyset,Java,Arrays,Arraylist,Hashmap,Keyset,首先,这是一个作业,所以我更多的是寻求帮助,然后是编码答案(不想作弊!)。我的任务是创建一个处理火车/火车站网络的程序。我所停留的部分添加电台及其连接,并将这些连接作为字符串数组列表返回。我已经在下面列出了到目前为止的代码,以及作业的摘录(与我现在所讲的部分相关)。我整个周末都在为这件事苦苦挣扎,所以任何帮助都将不胜感激 这只是我需要编辑的接口的实现,“MyNetwork”类。我只是觉得我一直在兜圈子,甚至可能还没有走上正轨 作业中的 public interface Network {
public interface Network {
/**
* Add a station to the network.
* @param station The station to be added.
*/
public void addStation(String station);
/**
* Add a direct connection from one station to another.
* @pre both fromStation and toStation have already been added by the method
* addStation.
* @param fromStation The station from which the connection begins.
* @param toStation The station at which the connection ends.
*/
public void addConnection(String fromStation, String toStation);
/**
* Get a list of all stations directly connected to a given station.
* @pre fromStation has been added to the network by the method addStation.
* @param fromStation
* @return A list of all the stations to which there is a direct connection
* from fromStation.
*/
public String[] getConnections(String fromStation);
/**
* Search for a station in the network.
* @param station Station to be searched for,
* @return true if the Station exists in the network, false otherwise.
*/
public boolean hasStation(String station);
/**
* Get all stations in the network.
* @return An array containing all the stations in the network, with no
* duplicates.
*/
public String[] getStations();
创建一个实现网络接口的类MyNetwork
此类的getConnections方法应返回一个数组,该数组仅包含直接连接到fromStation参数的站。
提示1:您可以使用HashMap实现这一点,键是字符串(表示站点),值是字符串的ArrayList(表示直接连接到的站点)
提示2:尽管getConnections方法返回字符串数组,但是HashMap中的值最好是字符串数组列表
接口强>
public interface Network {
/**
* Add a station to the network.
* @param station The station to be added.
*/
public void addStation(String station);
/**
* Add a direct connection from one station to another.
* @pre both fromStation and toStation have already been added by the method
* addStation.
* @param fromStation The station from which the connection begins.
* @param toStation The station at which the connection ends.
*/
public void addConnection(String fromStation, String toStation);
/**
* Get a list of all stations directly connected to a given station.
* @pre fromStation has been added to the network by the method addStation.
* @param fromStation
* @return A list of all the stations to which there is a direct connection
* from fromStation.
*/
public String[] getConnections(String fromStation);
/**
* Search for a station in the network.
* @param station Station to be searched for,
* @return true if the Station exists in the network, false otherwise.
*/
public boolean hasStation(String station);
/**
* Get all stations in the network.
* @return An array containing all the stations in the network, with no
* duplicates.
*/
public String[] getStations();
实现:
public class MyNetwork implements Network {
@Override
public void addStation(String station) {
ArrayList<String> Stations = new ArrayList<>();
Stations.add(station);
}
@Override
public void addConnection(String fromStation, String toStation) {
Map<String,String> Connections = new HashMap<>();
Connections.put(fromStation, toStation);
}
@Override
public String[] getConnections(String fromStation) {
return null; // dummy value!
}
@Override
public boolean hasStation(String station) {
return false; // dummy value!
}
@Override
public String[] getStations() {
return null; // dummy value!
}
}
公共类MyNetwork实现网络{
@凌驾
公共站(字符串站){
ArrayList Stations=新的ArrayList();
车站。添加(车站);
}
@凌驾
public void addConnection(String fromStation、String toStation){
Map Connections=newhashmap();
连接。放置(从站、到站);
}
@凌驾
公共字符串[]getConnections(来自Station的字符串){
返回null;//伪值!
}
@凌驾
公共布尔站(字符串站){
返回false;//伪值!
}
@凌驾
公共字符串[]getStations(){
返回null;//伪值!
}
}
您的网络需要有一个状态,使用一个或多个实例字段
就目前而言,它没有任何状态。每个方法都创建一个List或Map类型的局部变量,向该列表或Map添加一些内容,然后返回。因此,列表或映射直接超出范围并被垃圾收集
private Map<String, List<String>> stations = new HashMap<>();
// now all your methods should use the above map.
private Map stations=new HashMap();
//现在,所有方法都应该使用上面的映射。
请参见为了清楚起见,将使用输入流将“电台”读入程序,但这是本作业的下一步。再次感谢!谢谢你,JB!我想我已经得到了你的建议,这肯定比我开始的更有意义。所以,对于“addConnections”方法,我应该参考地图“stations”,然后执行类似“put..From/to”的操作?不太可能。您必须获取已连接到站点的现有站点列表,并将给定的已连接站点添加到列表中。我更正了我的答案。地图应为
地图
,将连接的站点列表与站点相关联。