示例#1
0
 // Option 10
 public void removeCity(String c) {
   City city = null;
   for (int i = 0; i < numCities; i++) {
     if (cities[i].name().equals(c)) {
       city = cities[i];
     }
   }
   if (city == null) {
     System.out.println("Invalid city choice");
     return;
   }
   // Remove all routes connected to the city
   for (Route r : adj[city.id() - 1]) {
     City other = r.other(city);
     adj[other.id() - 1].remove(r);
     routes.remove(r);
     numRoutes--;
   }
   cities[city.id() - 1] = null;
   adj[city.id() - 1] = null;
   numCities--;
   // Shift and resize arrays as necessary
   shiftCities(city.id() - 1);
   shiftAdj(city.id() - 1);
   if (numCities < cities.length / 2) { // halve the lengths of the arrays
     resizeCities(cities.length / 2);
     resizeAdj(cities.length / 2);
   }
 }