示例#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);
   }
 }
示例#2
0
 // Option 9
 public void addCity(String c) {
   if (c == null || c.length() == 0) {
     System.out.println("Invalid city choice");
     return;
   }
   City newCity = new City(numCities + 1, c);
   // Add to cities and adj
   if (numCities >= cities.length) {
     resizeCities(2 * cities.length);
     resizeAdj(2 * cities.length);
   }
   cities[numCities] = newCity;
   adj[numCities] = new Bag<Route>();
   numCities++;
 }