简体   繁体   English

从对象数组列表中获取最大值

[英]Getting max value from an arraylist of objects

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.我有一个名为城市的数组列表,其中包含来自另一个类的对象 City,并且该对象既包含所述城市的名称,也包含人口。 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.在一种方法中,我想总结所有城市人口,也就是得到这个国家的人口,但是我在 .parseLong 方法中遇到了一个例外,就像我正在做的那样。 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;
    }
    
}

About your Methode getTotalPop()关于您的 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. c 应该是一个长变量并初始化为 0,因为现在您正在尝试增加一个不可能的字符串。
  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();你正在迭代一个城市列表,所以 iter.next() 会给你一个城市,但你想要这个城市的人口,所以调用 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.我的建议是使用增强的 for/foreach 循环。

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):或者这个(增强的 for/foreach 循环):

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

About your Methode getLargest()关于您的 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.有一些方法可以完成这项工作,您可以使用初始化为 0 的变量最大弹出,并将每个城市人口与此变量进行比较,如果它更大,则将其设置为弹出。 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:此外,因为除了新的 ArrayList 之外,您没有在构造函数中初始化城市,所以不要在构造函数中这样做,而是像这样:

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()将 pop 初始化为 getTotalPop() 的结果是完全有意义的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM