我的可比接口逻辑有什么问题? [英] What is wrong with my comparable interface logic?

查看:56
本文介绍了我的可比接口逻辑有什么问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以问题如下

唯一的ID被分配给每个进入队列的学生.队列根据以下条件(优先条件)为学生服务:

A unique id is assigned to each student entering the queue. The queue serves the students based on the following criteria (priority criteria):

具有最高累积平均成绩(CGPA)的学生被送达.

The student having the highest Cumulative Grade Point Average (CGPA) is served first.

所有具有相同CGPA的学生都将按姓名区分大小写的字母顺序升迁.

Any students having the same CGPA will be served by name in ascending case-sensitive alphabetical order.

任何具有相同CGPA和名称的学生都将按照ID的升序排列

Any students having the same CGPA and name will be served in ascending order of the id

我的代码

class Priorities{
    public List<Students> getStudents(List<String> events) {
            PriorityQueue<Students> pq = new PriorityQueue<Students>();
            for ( String s : events) {
                if ( s.contains("ENTER")) {
                    String [] arr = s.split(" ");
                    int id = Integer.parseInt(arr[3]);
                    String name = arr[1];
                    Double cgpa = Double.parseDouble(arr[2]);       
                    pq.add(new Students(id, name, cgpa));
                }
                else
                    pq.poll();
            
            }
            List<Students> studentList = new ArrayList<Students>(pq);
    
            return studentList;
        }
        
        
        
    }
    
class Students implements Comparable<Students>{
    
    int id;
    String name;
    double cgpa;
    
    public Students(int id, String name, double cgpa) {
        this.id = id;
        this.name = name;
        this.cgpa = cgpa;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getCgpa() {
        return cgpa;
    }

    public void setCgpa(double cgpa) {
        this.cgpa = cgpa;
    }
        // -1 return left, 1 return right
    public int compareTo(Students other) {
    if ( this.equals(other))
        return 0;
    else if ( this.getCgpa() > other.getCgpa())
        return -1;
    else if ( this.getCgpa() < other.getCgpa())
        return 1;
    else if ( this.getCgpa() == other.getCgpa() && this.getName().compareTo(other.getName()) == 0)
        return Integer.compare(this.getId(), other.getId());
    else 
        return this.getName().compareTo(other.getName());
    
        
}
}

样本输入

12
ENTER John 3.75 50
ENTER Mark 3.8 24
ENTER Shafaet 3.7 35
SERVED
SERVED
ENTER Samiha 3.85 36
SERVED
ENTER Ashley 3.9 42
ENTER Maria 3.6 46
ENTER Anik 3.95 49
ENTER Dan 3.95 50
SERVED

样本输出

Dan
Ashley
Shafaet
Maria

我得到以下内容

Dan
Ashley
Maria
Shafaet

使用

List<Students> studentList = new ArrayList<Students>();
        while(!pq.isEmpty())
        {
            studentList.add(pq.poll());
        }

代替List studentList = new ArrayList(pq);帮助将PQ的确切顺序复制到列表中.

instead of List studentList = new ArrayList(pq); helps with copying the exact order of the PQ to the list.

推荐答案

比较器的一般结构应为:compare a field;如果字段值不同,则返回;否则,返回0.如果它们相同,请继续下一个字段.

The general structure of a comparator should be: compare a field; if the field values differ, return; if they are the same, proceed to the next field.

在这种情况下,它可能类似于:

In this case, it could look something like:

int cmp;

cmp = Double.compare(other.getCgpa(), this.getCgpa());
if (cmp != 0) return cmp;

cmp = this.getName().compareTo(other.getName());
if (cmp != 0) return cmp;

cmp = Integer.compare(this.getId(), other.getId());
if (cmp != 0) return cmp;

return 0;

(最后一个 if return 可以折叠为 return cmp; ;但我认为以后进行扩展更容易如上所示,因为您可以随后插入另一个 cmp/if .)

(The last if and return could be collapsed to just return cmp;; but I think it's easier to extend later if you do it as above, because you can then just insert another cmp/if.)

这篇关于我的可比接口逻辑有什么问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆