【问题标题】:Adding and Removing elements添加和删​​除元素
【发布时间】:2017-03-02 03:03:50
【问题描述】:

我应该让教授担任课程的讲师。该方法上方的评论也说明了目标。但是,我对如何去做这件事感到困惑。我是使用 java 编程的新手,所以我决定通过“instanceof”比较来检查教授是否在课程中。我是朝着正确的方向前进还是错过了什么?谢谢!

  • 接近尾声时,我添加了我刚刚遇到的 2 个其他方法,我相信我错误地实现了我的 getStudents。最后两种方法要求添加和删除学生。我不确定学生的键值对是什么。

    public class Course 实现 Comparable {

    /**
     * course id (unique)
     */
    private int id;
    /**
     * course name
     */
    private String name;
    /**
     * course level
     */
    private int level;
    /**
     * professor teaching the course, null if none
     */
    private String professor;
    /**
     * students enrolled in the course (unique), empty if none
     */
    private HashSet<String> students;
    
    /**
     * Create a course. Initially there is no professor or student for the
     * course.
     *
     * @param id course id
     * @param name course name
     * @param level course level
     */
    public Course(int id, String name, int level) {
        this.id = id;
        this.name = name;
        this.level = level;
        this.professor = null;
        this.students = new HashSet<>();
    }
    
    /**
     * Get the course id.
     *
     * @return course id
     */
    public int getId() {
        return id;
    }
    
    /**
     * Get the course name.
     *
     * @return course name
     */
    public String getName() {
        return name;
    }
    
    /**
     * Get the course level.
     *
     * @return course level
     */
    public int getLevel() {
        return level;
    }
    
    /**
     * Get the professor teaching the course.
     *
     * @return the professor, if none, null
     */
    public String getProfessor() {
        return professor;
    }
    
    /**
     * Get the students enrolled in the course.
     *
     * @return the students, organized by ascending hash codes. if there are no
     * students enrolled the list should be empty.
     */
    public Collection<String> getStudents() {
        return students;
    }
    
    /**
     * Make a professor the instructor of this course. If another professor was
     * teaching it, that information is lost.
     *
     * @param username the username of the professor
     */
    public void addProfessor(String username) {
    
    }
    

    } /**

    • 将学生添加到课程中,如果他们没有注册,
    • 在恒定时间内。 *
    • @param username 学生的用户名
    • @return 是否添加了学生 */ 公共布尔addStudent(字符串用户名){ //去做 返回假; }

    /**

    • 从课程中删除学生(如果他们已注册),
    • 在恒定时间内。 *
    • @param username 要删除的学生的用户名
    • @return 如果学生被删除,则返回 true,如果学生不在课程中,则返回 false */ 公共布尔删除学生(字符串用户名){ // 去做 返回假; }

【问题讨论】:

  • addProfessor 方法的注释说以前的教授会被新教授覆盖,所以你真的要检查教授是否已经在课程中了吗?
  • 关于添加和删除学生方法的实现,请看一下 Set 上的添加和删除方法。 docs.oracle.com/javase/8/docs/api/java/util/Set.html

标签: java


【解决方案1】:

您使用 instanceof 关键字在运行时检查对象是否属于特定类型。在这种情况下,注释指出如果先前设置了一个值,它就会丢失。这意味着您可以简单地将给定值设置为教授而不进行任何检查

public void addProfessor(String userName) {
    professor = userName;
}

【讨论】:

  • 如果有前任教授,默认情况下会删除吗?
  • 是的,它只会覆盖之前的值
  • 不确定您是否看到我刚刚添加的部分,但您能解释一下 Student 是如何成为 HashSet 的吗?我对键值对是什么感到困惑。
  • @JanSolo 上面的代码将学生变量作为 HashSet。集合没有键值对(这将是一个映射)。集合是无序(通常,除了少数特殊类型)的数据集合。它允许快速检查元素是否包含在集合中。集合中的每个元素都必须是唯一的。因此,如果您有 {"Aaron Davis", "Jan Solo"} 并尝试添加另一个“Jan Solo”实例,它不会更改集合并返回“false”。基本上,这就是说,在课堂上,每个学生都必须有一个唯一的字符串标识符(可能是全名或学生 ID 等)。
  • @JanSolo 需要注意的一件事:上面 getStudents 的实现是不正确的。 cmets 方法声明它应该返回一个排序的学生列表,但您返回的是支持哈希集。您需要做的是创建一个数组列表,将哈希集中的学生添加到列表中并对其进行排序。然后返回该排序列表。有许多流和集合方法可以帮助做到这一点。
【解决方案2】:

只需实现方法 addProfessor -

public void addProfessor(String username) {
    /*
    This assignment will replace any other professor
    who are teaching of this course.
    If you assign the class variable **professor**,
    when you call getProfessor(), that will return
    username this professor.
    */
    this.professor = username;
}

【讨论】:

    【解决方案3】:

    您可以在professor 上调用getter 来检查它是否已经设置。

    public void addProfessor(String professor) {
         if(this.getProfessor() == null || this.getProfessor().isEmpty()) {
             this.setProfessor(professor);
         }
    }
    

    【讨论】:

    • 方法 cmets 声明,如果之前将值设置为教授并不重要。
    • @AaronDavis,是的。我认为这是 OP 面临的问题。这就是为什么他将其作为对该方法的评论。
    • 您似乎添加了一个额外的参数并删除了空白。是否有另一种方法可以在不编辑原始方法的情况下实现这一点?不确定我的教授是否会以任何方式对其进行修改。
    • @JanSolo,刚刚编辑了我的答案,使其适用于您现有的 addProessor 方法。看看吧。
    猜你喜欢
    • 2016-12-27
    • 2020-09-05
    • 2019-03-23
    • 2015-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多