Home > Mobile >  Getting max value from an arraylist of objects
Getting max value from an arraylist of objects

Time:07-04

I have an arraylist called cities with objects City from another class, and this object contains both the name of said city but also population. In one method I wanna sum all city populations, aka get the population of the country, however I'm getting an exception in .parseLong method the way I'm doing it. In another one, I wanna check what city has the largest population, but I'm not getting anything when I print and don't know how to fix it. Basically I don't know how to get the value of the objects inside the arraylist. Commented where I have issues for better understanding. Help's appreciated!

public class Country {
    private String name;
    private City capital;
    private int pop;
    private ArrayList<City> cities;

    public Country(String name, Cidade capital, int pop) {
        this.name = name;
        this.capital = capital;
        this.pop = pop;
        cities = new ArrayList<>();
        cities.add(capital);
    }
    public long getTotalPop(){
        String c = null;
        Iterator<City> iter = cities.iterator();
        while(iter.hasNext()){
            c  = iter.next();
            long s = Long.parseLong(c); //giving exception here
            System.out.println(s);
            return s;
        }
        return 0;
    }
    
    public City getLargest(){
        for(City city: cities){
            if(city.getPop()>city.getPop()){ //method is fine but if is very wrong since am not sure what to compare to
                return city;
            }
        }
        return null;
    }
    
}

public class City {
    private String name;
    private int pop;

    public City(String name) {
        this.name = name;
    }

    public City(String name, int pop) {
        this.name = nome;
        this.pop = pop;
    }

    public String getName() {
        return name;
    }

    public int getPop() {
        return pop;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setPop(int pop) {
        this.pop = pop;
    }
    
}

CodePudding user response:

About your Methode getTotalPop()

  1. c should be a long variable and in initialized to 0 because now you are trying to increment a string which is impossible.
  2. you are iterating about a list of cities so iter.next() will give you a city but you want the population of this city so call c = iter.next().getPop();
  3. You don't have to use an iterator. It is okay but you won't get a benefit in this case. My recommendation use a enhanced for/foreach loop.

So use this(iterator):

public long getTotalPop(){
    long result = 0;
    Iterator<City> iter = cities.iterator();
    
    while(iter.hasNext()){
        result  = iter.next().getPop();
    }
    return result;
}

or this(enhanced for/foreach loop):

public long getTotalPop(){
    long result = 0;
    
    for (City city : cities) {
        result  = city.getPop();
    }
    return result;
}

About your Methode getLargest()

There are some ways to do the job you could use an variable largest pop initialized to 0 and compare each City population with this variable and set it to the pop if it is greater. Or set your pop of the first City as the value to be compared.

With first City:

public City getLargest() {
    if (!cities.isEmpty()) {
        City largest = cities.get(0);
        for (int i = 1; i < cities.size(); i  ) {
            City city = cities.get(i);
            if (largest.getPop() < city.getPop()) {
                largest = city;
            }
        }
        return largest;
    }
    return null;
}

Furthermore because you don't initialize cities in the construtor other than to the new ArrayList don't do that in the constructor but like this:

private List<City> cities = new ArrayList<>();

public Country(String name, City capital, int pop) {
    this.name = name;
    this.capital = capital;
    this.pop = pop;
    this.cities.add(capital);
}

At last why is the pop of the country not the same as the pop of all of the cities it has? It would make totaly sense to initialize pop to the result of getTotalPop()

  •  Tags:  
  • java
  • Related