如何设置一个对象的变量的值在一个ArrayList

编程入门 行业动态 更新时间:2024-10-10 23:15:29
本文介绍了如何设置一个对象的变量的值在一个ArrayList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我的工作分配中,我有:

  • 创建具有以下属性/变量的Employee类:名称年龄部门

  • 创建一个名为Department类其中将包含员工的列表。

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

    乙。系数值只能是下列之一:

    • 会计学
    • 市场营销
    • 人力资源
    • 信息系统
  • 我有困难的时候试图找出如何完成2B。以下是我迄今为止:

    进口的java.util。*;公共类Employee {字符串名称;INT年龄;串部门;员工(字符串名称,诠释年龄,弦乐部){    this.name =名称;    this.age =年龄;    this.department =部;}INT getAge(){    返回年龄;}}类系{公共静态无效的主要(字串[] args){    清单<员工> empList =新的ArrayList<员工>();    Collections.sort(empList,新的比较<员工>(){        公众诠释比较(员工E1,E2员工){            返回新的整数(e1.getAge())的compareTo(e2.getAge());        }    });    }}

    解决方案

    您可以使用,这将限制你只能使用指定的值相同的目的枚举。声明你的处枚举如下:

    公共枚举系{    会计,Marketting,Human_Resources,Information_Systems}

    现在

    您员工类可以是

    公共类Employee {    字符串名称;    INT年龄;    系部;    员工(字符串名称,诠释年龄,系部){        this.name =名称;        this.age =年龄;        this.department =部;    }    INT getAge(){        返回年龄;    }}

    和同时创造员工,你可以使用

    员工员工=新员工(普拉萨德,47,Department.Information_Systems);

    修改按阿德里安深当然,因为它是一个伟大的建议的建议。

    • 的枚举是这就是为什么其良好的大写字母根据Java约定声明常量。
    • 但是,我们不希望枚举的资本重新presentation待观察,所以我们可以创建枚举构造,并通过读取信息给它。
    • 我们西港岛线修改枚举包括的toString()方法和构造,它接受一个字符串参数。

      公共枚举系{   会计(会计),MARKETTING(Marketting),HUMAN_RESOURCES(        人力资源),INFORMATION_SYSTEMS(信息系统);   私人字符串DEPTNAME;    部(字符串DEPTNAME){       this.deptName = DEPTNAME;    }   @覆盖   公共字符串的toString(){    返回this.deptName;   }}

    所以,当我们正在创建一个员工对象,如下所示,使用它

    员工员工=新员工(普拉萨德Kharkar,47,Department.INFORMATION_SYSTEMS);的System.out.println(employee.getDepartment());

    我们会得到一个可读的字符串重新presentation为信息系统因为它是由返回的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())pareTo(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 and constructor 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

    本文发布于:2023-11-24 11:48:29,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1625083.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:变量   如何设置   对象   ArrayList

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!