【问题标题】:How would I find the max value of objects in ArrayList in for-loop? -Java [closed]如何在 for 循环中找到 ArrayList 中对象的最大值? -Java [关闭]
【发布时间】:2017-01-29 04:09:50
【问题描述】:

看来我的 Persons 类中的两个方法都给了我错误的信息。我有两个方法叫做 get totalSalary()maxSalary(),它们通过 ArrayList personList 计算总工资或所有对象,然后另一个找到最大工资。这是我的 Persons 类,其中包含我在查找最大值和总数方面的所有尝试:

import java.util.*;

public class Persons {

public ArrayList<Person> personsList = new ArrayList<Person>();

public boolean addPerson(Person newPerson) {
    personsList.add(newPerson);
    return true;
}


public double totalSalary() {
    double salary = 0;
    for(Person person : personsList) {
        salary += person.getJob().getSalary();
    }
    return salary;
}


public double maxSalary() {
    double max = 0.0;
    for(Person person : personsList) {
        if(person.getJob().getSalary() > max) {
            max = person.getJob().getSalary();
        }
     }
    return max;
 }

}

这是我创建对象、添加信息并调用我的方法的主要位置:

import java.util.*;
import java.util.ArrayList;

public class testPersons {

public static void main(String[] args) {

    Persons persons = new Persons();

    Address person1Address = new Address(1052, "Sum St", "San Francisco", "CA", "94544");
    Address person1JobAddress = new Address(1542, "High St", "Santa Cruz", "CA", "94063");
    ArrayList<String> person1Phone = new ArrayList<String>();
    person1Phone.add("650-555-555");
    Job person1Job = new Job("Teacher", 10000.00, person1JobAddress);
    Person person1 = new Person("Dylan Johnson", "San Mateo", 'M', person1Address, person1Job, person1Phone);

    Address person2Address = new Address(1054, "Pico St", "Los Angeles", "CA", "97556");
    Address person2JobAddress = new Address(5435, "James St", "Redwood City", "CA", "94063");
    ArrayList<String> person2Phone = new ArrayList<String>();
    person2Phone.add("555-555-555");
    Job person2Job = new Job("Mechanic", 20000.00, person2JobAddress);
    Person person2 = new Person("Rollan Tico", "New York", 'M', person2Address, person2Job, person2Phone);

    Address person3Address = new Address(517, "A St", "Redwood City", "CA", "94063");
    Address person3JobAddress = new Address(519, "Bing St", "San Carlos", "CA", "94064");
    ArrayList<String> person3Phone = new ArrayList<String>();
    person3Phone.add("555-555-555");
    Job person3Job = new Job("Janitor", 5000.00, person2JobAddress);
    Person person3 = new Person("Dwayne Rock", "San Jose", 'M', person2Address, person2Job, person2Phone);

    persons.addPerson(person1);
    persons.addPerson(person2);
    persons.addPerson(person3);

    System.out.printf("The total salaries: "+ persons.totalSalary() + "\n");
    System.out.printf("The max salary: " + persons.maxSalary() + "\n");
  }
}

【问题讨论】:

  • 你的输出是什么?
  • getJob() & getSalary() 是如何实现的?可以分享一下吗?
  • 您能告诉我们您的预期输出和实际输出吗?
  • 使用builder pattern 可以防止这种错字归类错误的发生。

标签: java class object for-loop arraylist


【解决方案1】:

您正在为第 2 个人和第 3 个人添加 person2 数据。

改变

Person person3 = new Person("Dwayne Rock", "San Jose", 'M', person2Address, person2Job, person2Phone); 

收件人:

Person person3 = new Person("Dwayne Rock", "San Jose", 'M', person3Address, person3Job, person3Phone); 

【讨论】:

  • 这会导致完全错误,但不会导致最大错误。 OP声称两者都是错误的。
【解决方案2】:

除了 person3 的变量分配错误外,一切都运行良好,请查看以下内容。

public class testPersons {

public static void main(String[] args) {

    Persons persons = new Persons();

    Address person1Address = new Address(1052, "Sum St", "San Francisco", "CA", "94544");
    Address person1JobAddress = new Address(1542, "High St", "Santa Cruz", "CA", "94063");
    ArrayList<String> person1Phone = new ArrayList<String>();
    person1Phone.add("650-555-555");
    Job person1Job = new Job("Teacher", 10000.00, person1JobAddress);
    Person person1 = new Person("Dylan Johnson", "San Mateo", 'M', person1Address, person1Job, person1Phone);

    Address person2Address = new Address(1054, "Pico St", "Los Angeles", "CA", "97556");
    Address person2JobAddress = new Address(5435, "James St", "Redwood City", "CA", "94063");
    ArrayList<String> person2Phone = new ArrayList<String>();
    person2Phone.add("555-555-555");
    Job person2Job = new Job("Mechanic", 20000.00, person2JobAddress);
    Person person2 = new Person("Rollan Tico", "New York", 'M', person2Address, person2Job, person2Phone);

    Address person3Address = new Address(517, "A St", "Redwood City", "CA", "94063");
    Address person3JobAddress = new Address(519, "Bing St", "San Carlos", "CA", "94064");
    ArrayList<String> person3Phone = new ArrayList<String>();
    person3Phone.add("555-555-555");
    Job person3Job = new Job("Janitor", 5000.00, person3JobAddress);
    Person person3 = new Person("Dwayne Rock", "San Jose", 'M', person3Address, person3Job, person3Phone);

    persons.addPerson(person1);
    persons.addPerson(person2);
    persons.addPerson(person3);

    System.out.printf("The total salaries: "+ persons.totalSalary() + "\n");
    System.out.printf("The max salary: " + persons.maxSalary() + "\n");
  }
}

【讨论】:

    猜你喜欢
    • 2020-03-04
    • 2021-10-20
    • 2020-02-26
    • 1970-01-01
    • 2021-01-10
    • 2018-03-06
    • 1970-01-01
    • 2017-09-30
    • 1970-01-01
    相关资源
    最近更新 更多