【问题标题】:How to set a value of a variable of an object in an ArrayList如何在 ArrayList 中设置对象的变量值
【发布时间】:2013-07-10 12:30:24
【问题描述】:

我正在执行一项任务,我必须:

  1. 使用以下属性/变量创建一个 Employee 类: 姓名 年龄 部门

  2. 创建一个名为 Department 的类,该类将包含员工列表。

    一个。 Department 类将有一个方法,该方法将返回按年龄排序的员工。

    b. Department 的值只能是以下之一:

    • “会计”
    • “营销”
    • “人力资源”
    • “信息系统”

我很难弄清楚如何完成 2b。 这是我目前所拥有的:

import java.util.*;

public class Employee {
String name; 
int age;
String department;

Employee (String name, int age, String department) {
    this.name = name;
    this.age = age;
    this.department = department;

}
int getAge() {
    return age;
}
}

class Department {
public static void main(String[] args) {
    List<Employee>empList = new ArrayList<Employee>();

    Collections.sort (empList, new Comparator<Employee>() {
        public int compare (Employee e1, Employee e2) {
            return new Integer (e1.getAge()).compareTo(e2.getAge());
        }
    });
    }
}   

【问题讨论】:

  • " b. Value of Department can be only one of the following: "Accounting" "Marketing" "Human Resources" "Information Systems"" -- 对此属性使用 enum 类型,而不是字符串。这将强制 Java 只允许此变量或参数类型的特定项。
  • 欲了解更多信息,请查看Enum Types
  • 下次请仅提供与您的问题相关的信息。整堆代码和你的作业的大部分描述与你的问题无关。

标签: java arraylist


【解决方案1】:

您可以将枚举用于相同的目的,这将限制您仅使用指定的值。 按如下方式声明您的 Department 枚举

public enum Department {

    Accounting, Marketting, Human_Resources, Information_Systems

}

Employee 班级现在可以

public class Employee {
    String name;
    int age;
    Department department;

    Employee(String name, int age, Department department) {
        this.name = name;
        this.age = age;
        this.department = department;

    }

    int getAge() {
        return age;
    }
}

在创建员工时,您可以使用

Employee employee = new Employee("Prasad", 47, Department.Information_Systems);

EDITAdrian Shum 建议,当然因为这是一个很好的建议。

  • 枚举是常量,这就是为什么根据 java 约定用大写字母声明它的好处。
  • 但我们不希望看到枚举的大写表示,因此我们可以创建枚举构造函数并将可读信息传递给它。
  • 我们将修改枚举以包含toString() 方法和constructor,它接受一个字符串参数。

     public enum Department {
    
       ACCOUNTING("Accounting"), MARKETTING("Marketting"), HUMAN_RESOURCES(
            "Human Resources"), INFORMATION_SYSTEMS("Information Systems");
    
       private String deptName;
    
        Department(String deptName) {
           this.deptName = deptName;
        }
    
       @Override
       public String toString() {
        return this.deptName;
       }
    
    }
    

所以当我们如下创建Employee对象并使用它时,

Employee employee = new Employee("Prasad Kharkar", 47, Department.INFORMATION_SYSTEMS);
System.out.println(employee.getDepartment()); 

我们将得到一个可读的字符串表示为Information Systems,因为它是由toString() 方法返回的,该方法由System.out.println() 语句隐式调用。 阅读关于Enumerations的好教程

【讨论】:

  • 哦哇...现在更有意义了。非常感谢!
  • 这是一个优雅的解决方案。
  • 只是一个建议,在Java约定中,枚举的命名应该遵循常量,也就是说,值应该是ACCOUNTING, MARKETING, HUMAN_RESOURCES, INFORMATION_SYSTEMS。如果需要,您可以为枚举添加一个方法以提供人类可读的名称。
猜你喜欢
  • 2018-06-09
  • 2011-01-25
  • 2021-11-17
  • 1970-01-01
  • 2021-02-06
  • 2014-12-04
  • 2018-07-31
  • 2016-03-04
  • 1970-01-01
相关资源
最近更新 更多