如何在ArrayList中设置对象的变量值 [英] How to set a value of a variable of an object in an ArrayList
问题描述
我正在完成一项任务,我必须:
创建一个具有以下属性/变量的 Employee 类:姓名年龄部门
创建一个名为 Department 的类,其中将包含员工列表.
一个.Department 类将有一个方法可以返回按年龄排序的员工.
B.Department 的值只能是以下之一:
- 会计"
- 营销"
- 人力资源"
- 信息系统"
我最难的是弄清楚如何完成 2b.这是我目前所拥有的:
import java.util.*;公共类员工{字符串名称;年龄;弦部;员工(字符串名称,整数年龄,字符串部门){this.name = 名称;this.age = 年龄;this.department = 部门;}int getAge() {回归年龄;}}课部{公共静态无效主(字符串 [] args){ListempList = new ArrayList();Collections.sort (empList, new Comparator() {公共 int 比较(员工 e1,员工 e2){返回新整数 (e1.getAge()).compareTo(e2.getAge());}});}}
您可以将枚举用于相同的目的,这将限制您只能使用指定的值.声明你的 Department
枚举如下
公共枚举部门{会计、市场营销、人力资源、信息系统}
你的Employee
类现在可以
公共类员工{字符串名称;年龄;部门部门;员工(字符串名称,整数年龄,部门部门){this.name = 名称;this.age = 年龄;this.department = 部门;}int getAge() {回归年龄;}}
在创建员工时,您可以使用
Employee employee = new Employee("Prasad", 47, Department.Information_Systems);
EDIT 按照 Adrian Shum 的建议,当然因为这是一个很好的建议.
- 枚举是常量,这就是为什么根据java约定用大写字母声明是好的.
- 但是我们不希望看到枚举的大写表示,因此我们可以创建枚举构造函数并将可读信息传递给它.
我们将修改枚举以包含
toString()
方法和采用字符串参数的constructor
.公共枚举部门{会计(会计"),营销(营销"),HUMAN_RESOURCES("人力资源"), INFORMATION_SYSTEMS("信息系统");私人字符串部门名称;部门(字符串部门名称){this.deptName = 部门名称;}@覆盖公共字符串 toString() {返回 this.deptName;}}
所以当我们创建一个 Employee
对象并使用它时,
Employee employee = new Employee("Prasad Kharkar", 47, Department.INFORMATION_SYSTEMS);System.out.println(employee.getDepartment());
我们将得到一个可读的字符串表示,如 Information Systems
,因为它由 toString()
方法返回,该方法被 System.out.println()
语句.阅读关于枚举的优秀教程>
I am working on an assignment in which I have to:
Create an Employee class with the following attributes/variables: name age department
Create a class called Department which will contain a list of employees.
a. Department class will have a method which will return its employees ordered by age.
b. Value of Department can be only one of the following:
- "Accounting"
- "Marketing"
- "Human Resources"
- "Information Systems"
I am having the hardest time trying to figure out how to complete 2b. Here is what I have so far:
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());
}
});
}
}
You can use enumerations for the same purpose which will restrict you to use only specified values.
Declare your Department
enum as follows
public enum Department {
Accounting, Marketting, Human_Resources, Information_Systems
}
You Employee
class can now be
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;
}
}
and while creating employee, you can use
Employee employee = new Employee("Prasad", 47, Department.Information_Systems);
EDIT as suggested by Adrian Shum and of course because it is a great suggestion.
- The enums are constants thats why its good to be declared in capital letters according to java conventions.
- But we don't want the capital representation of the enums to be seen so we can create enum constructors and pass readable info to it.
We wil modify enum to include
toString()
method andconstructor
which takes a string argument.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; } }
So when we are creating an Employee
object as follows and using it,
Employee employee = new Employee("Prasad Kharkar", 47, Department.INFORMATION_SYSTEMS);
System.out.println(employee.getDepartment());
We will get a readable string representation as Information Systems
as it is returned by toString()
method which is called implicitly by System.out.println()
statement.
Read the good tutorial about Enumerations
这篇关于如何在ArrayList中设置对象的变量值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!