import java.util.*; import java.io.*; public class Subway { private Map<String, Set<String>> lines; // 存储地铁线路信息 private Map<String, Set<String>> stations; // 存储地铁站点信息 public Subway() { lines = new HashMap<>(); stations = new HashMap<>(); } // 从文件中读取地铁线路信息 public void loadFromFile(String filename) { try { FileReader reader = new FileReader(filename); BufferedReader br = new BufferedReader(reader); String line = null; while ((line = br.readLine()) != null) { String[] fields = line.split("\\|"); String lineName = fields[0]; String[] stations = fields[1].split(" "); Set<String> stationsSet = new HashSet<>(Arrays.asList(stations)); lines.put(lineName, stationsSet); for (String station : stations) { Set<String> linesSet = this.stations.get(station); if (linesSet == null) { linesSet = new HashSet<>(); } linesSet.add(lineName); this.stations.put(station, linesSet); } } br.close(); reader.close(); } catch (IOException ex) { ex.printStackTrace(); } } // 打印所有的地铁线路信息 public void print() { for (String line : lines.keySet()) { System.out.print(line + ": "); Set<String> stations = lines.get(line); for (String station : stations) { System.out.print(station + " "); } System.out.println(); } } } // 在主程序中调用Subway类来导入地铁线路信息并展示在界面上 public class MainActivity extends AppCompatActivity { private Subway subway; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); subway = new Subway(); subway.loadFromFile("beijing_subway.txt"); subway.print(); // 将地铁线路信息展示在界面上 // ... } }
标签:24,总结,String,stations,2023.3,station,new,line,public From: https://www.cnblogs.com/wllovelmbforever/p/17253467.html