【问题标题】:HashMap as an ArrayList of StringsHashMap 作为字符串的 ArrayList
【发布时间】:2014-02-26 00:42:53
【问题描述】:

首先这是一项任务,所以我更多的是寻求帮助,然后是编码答案(不想作弊!)。我的任务是创建一个程序来处理车站的火车/铁路网络。我坚持的部分添加了站点、它们的连接,并将这些连接作为字符串数组列表返回。我已经在下面包含了到目前为止的代码,以及作业的摘录(与我现在正在讨论的部分相关)。我整个周末都在努力解决这个问题,所以任何帮助都将不胜感激。

这只是我需要编辑的接口的实现,“MyNetwork”类。我只是觉得我一直在兜圈子,可能连右脚都没有下车?

来自作业;

创建一个实现 Network 接口的 MyNetwork 类。

此类的 getConnections 方法应返回一个数组,该数组仅包含那些直接连接到 fromStation 参数的站。 提示 1:您可以使用 HashMap 来执行此操作,键是字符串(表示站点),值是字符串的 ArrayLists(表示有直接连接的站点)。

提示2:虽然getConnections方法返回的是字符串数组,但是HashMap中的值最好是字符串的ArrayLists

界面;

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!
    }
}

【问题讨论】:

  • 为了清楚起见,将使用输入流将“站”读入程序,但这是此分配的下一步。再次感谢!

标签: java arrays arraylist hashmap keyset


【解决方案1】:

您的网络需要有一个状态,使用一个或多个实例字段。

按原样,它没有任何状态。每个方法都会创建一个 List 或 Map 类型的局部变量,向该 List 或 Map 添加一些内容,然后返回。所以 List 或 Map 直接超出范围并被垃圾回收。

private Map<String, List<String>> stations = new HashMap<>();

// now all your methods should use the above map.

http://docs.oracle.com/javase/tutorial/java/javaOO/classes.html

【讨论】:

  • 感谢 JB!我想我已经得到了你的建议,而且它肯定比我开始的更有意义。那么,对于“addConnections”方法,我应该指的是地图“站”,然后做类似“put..From/to”之类的操作?
  • 并非如此。您必须获取已连接到站点的现有站点列表,并将给定的已连接站点添加到列表中。我更正了我的答案。地图应该是Map&lt;String, List&lt;String&gt;&gt;,将连接的站点列表与站点相关联。
猜你喜欢
  • 1970-01-01
  • 2014-04-15
  • 1970-01-01
  • 2020-07-26
  • 2014-04-03
  • 2016-06-28
  • 1970-01-01
  • 2017-06-08
  • 1970-01-01
相关资源
最近更新 更多