-1

I would like to convert the following code from array to any other way (the most important is effective) which means that there is infinite space and I will not have to set the length of the array.

How can this be done? How can I set up an unlimited cities? using LinkedList

  • The idea is that it is possible to define a certain country in which certain cities are stored (the name of the city, the city center, the central bus station,... - as in the picture below) - In my code MAX_NUM_CITIES = 1000;

enter image description here

My Code:

public class Country {
 //instance variables
 private String _countryName; // name of the country
 private City[] _cities; // Array of the cities 
 private int _noOfCities; //number of cities in a country

 public void CityArray() {
  _cities = new City[MAX_NUM_CITIES];
  _noOfCities = 0;
 }
 //constants:
 public final int MAX_NUM_CITIES = 1000;

 /**
  * Constructer for object in Country class construct Country with info accordingly
  * @param countryName represents the name of country
  * @param cities represents the cities array
  * @param noOfCities represents the number of cities
  */
 public Country(String countryName) {
  this._countryName = _countryName;
  this._noOfCities = _noOfCities;
  City[] cities = new City[MAX_NUM_CITIES];

 }
 boolean addCity(java.lang.String cityName, double XcityCenter, double YcityCenter, double XStationPoint, double YStationPoint, long numOfResidents, int numOfNeighborhoods) {
  if (_noOfCities <= MAX_NUM_CITIES) return false;
  _cities[_noOfCities++] = new City(cityName, XcityCenter, YcityCenter, XStationPoint, YStationPoint, numOfResidents, numOfNeighborhoods);
  return true;
 }

 public long getNumOfResidents() {
  long SumOfCities = 0;
  if (_noOfCities > 0) //empty Array           
  {
   SumOfCities = _cities[0].getNumOfResidents();
   for (int i = 1; i < _noOfCities; i++)
    SumOfCities += _cities[i].getNumOfResidents();
  } else
   SumOfCities = 0;
  return SumOfCities;
 }


 public String getCountryName() {
  return this._countryName;
 }

 public int getNumOfCities() {
  return this._noOfCities;
 }

 public City[] getCities() {
  int noOfCities = this._noOfCities;
  City[] cities = new City[noOfCities];
  for (int i = 0; i < _noOfCities; i++) cities[i] = new City(this._cities[i]);
  return cities;
 }

 public String toString() {
  if (_noOfCities == 0) //empty Array
   System.out.println("There are no cities in this country ");
  else
   for (int i = 0; i < _noOfCities; i++) _cities[i].toString();
  return toString();
 }
}
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
AnnaLA
  • 155
  • 1
  • 11

1 Answers1

3

I would step away from arrays if the length is:

  • unknown
  • can change

I suggest using one of the different List implementations from the JDK, specifically ArrayList and LinkedList.

The first uses an internal array which may be expanded if an element is added and would lead to the array being too small (it does this all by itself, so no need to worry).

The second is a node list, which means that for every element you add, a new (internal) node object is appended to the last node.

You'd of course have to change your code for this.

  1. Define your _cities to be a List<City>: private List<City> _cities
  2. Initialize that with the wanted implementation in the constructor: _cities = new ArrayList<>(); or _cities = new LinkedList<>();
  3. In your add method you can just call: _cities.add(new City(cityName, XcityCenter, YcityCenter, XStationPoint, YStationPoint, numOfResidents, numOfNeighborhoods));
  4. In your getNumOfResidents you can use the following snippet (which uses Java streaming api introduced in java 8):

    return _cities.stream()
        .mapToLong(City::getNumOfResidents)
        .sum();
    
  5. for getCities() you'd have to change the return type to List<City> and use the following: return new ArrayList<>(_cities) or return new LinkedList<>(_cities) depending on the implementation you want to use.

Lino
  • 19,604
  • 6
  • 47
  • 65
  • Could you Change my code (at least just the first part- public Country& boolean addCity) to LinkedList please?<3 – AnnaLA Jan 18 '19 at 12:24
  • @AnnaLA if you follow my instructions 1-5 it should be pretty easy for you to do that yourself – Lino Jan 18 '19 at 12:25