我创建了一个HashMap,其中每个键都包含一个ArrayList作为值。我无法理解如何获取与键关联的ArrayList并获取存储在其中的所有值。请参阅下面运行程序时出现的错误。
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Scanner;
public class Flights {
private HashMap<String, ArrayList<String>> flights = new HashMap<String, ArrayList<String>>();
private void readFlights(String filename) {
try {
BufferedReader bf = new BufferedReader(new FileReader(filename));
while (true) {
String line = bf.readLine();
if (line == null) {
break;
}
if (!line.isEmpty()) {
String fromCity = line.substring(0, line.indexOf("-"));
String toCity = line.substring(line.indexOf(">") + 2);
ArrayList<String> city = flights.get(fromCity);
if (city != null) {
city.add(toCity);
} else {
ArrayList<String> destinations = new ArrayList<String>();
destinations.add(toCity);
flights.put(fromCity, destinations);
}
}
}
bf.close();
} catch (IOException e) {
e.printStackTrace();
}
}
private void printCities() {
Iterator<String> fi = flights.keySet().iterator();
while (fi.hasNext()) {
String next = fi.next();
System.out.println(next + "-> "+ flights.get(next));
}
}
@SuppressWarnings("resource")
private void printWelcome() {
System.out
.println("Welcome to the flight planner.\nHere is a list of all of our cities:");
printCities();
System.out.print("Enter the starting city.");
Scanner in = new Scanner(System.in);
String input = in.nextLine();
System.out.println("from " + input + " you can fly directly to:");
printAvailableFlights(input);
}
private void printAvailableFlights(String city) {
ArrayList<String> origin = flights.get(city);
for (String cities: origin) {
System.out.println(cities);
}
}
public static void main(String[] args) {
Flights f = new Flights();
f.readFlights("flights.txt");
f.printWelcome();
}
}
这是flights.txt文件:
San Jose -> San Francisco
San Jose -> Anchorage
New York -> Anchorage
New York -> San Jose
New York -> San Francisco
New York -> Honolulu
Anchorage -> New York
Anchorage -> San Jose
Honolulu -> New York
Honolulu -> San Francisco
Denver -> San Jose
San Francisco -> New York
San Francisco -> Honolulu
San Francisco -> Denver
以下是我在运行程序时在控制台中看到的内容:
Welcome to the flight planner.
Here is a list of all of our cities:
Honolulu -> [New York, San Francisco]
Denver -> [San Jose]
Anchorage -> [New York, San Jose]
San Francisco -> [New York, Honolulu, Denver]
New York -> [Anchorage, San Jose, San Francisco, Honolulu]
San Jose -> [San Francisco, Anchorage]
Enter the starting city.Denver
from Denver you can fly directly to:
Exception in thread "main" java.lang.NullPointerException
at Flights.printAvailableFlights(Flights.java:64)
at Flights.printWelcome(Flights.java:59)
at Flights.main(Flights.java:72)
答案 0 :(得分:1)
这一行
String fromCity = line.substring(0, line.indexOf("-"));
在起始城市之后留下一个空格,因为这是您的文本文件的格式。您的密钥看起来像"Denver "
和"San Jose "
。您应该将其更改为:
String fromCity = line.substring(0, line.indexOf("-") - 1);
此外,使用整个分隔符" -> "
更有意义,因为有些城市中有短划线,例如Wilkes-Barre。
NullPointerException
,因为如果密钥不存在,Map
将返回null。您应该在printAvailableFlights
。