特定のステーションで開始または終了するネットワーク内の接続を含むコレクションを返そうとしています。私はそれを返す方法を理解するのに苦労し、駅のパラメータを取る。また、メソッドの中にhashMapを作成して、それを行う方法や、それを外部で作成する必要がありますか?コレクションを返す
これは、return文
CODEのために私にエラーincompatible types: Connection cannot be converted to Collection<Connection>
を与えている:
/**
* Return a Collection containing all the Connections in the network that
* start or end at a specified station
*
* @param station Station to/from which the Connection should run
*
* @return a Collection containing all the connections that start or end at
* the specified station
*/
@Override
public Collection<Connection> getConnectionsFrom(Station station) {
Map<Station, Connection> stationConnectionFrom = new HashMap<Station, Connection>();
return stationConnectionFrom.get(station);
}
まあ、はい、接続は、接続の集まりではありません。コレクションを返す場合は、コレクションを作成してその中にコネクションを挿入する必要があります。 – azurefrog
マップを作成したばかりの場合は、マップにはどのようになると思いますか? – flakes
a)作成したばかりのマップは空ですので、 '.get()'はnullを返します。 b) '.get()'は、Connectionのコレクションではなく、単一の 'Connection'オブジェクトとしてマップの値を宣言したため、' Connection'インスタンスを返します。 – dsh