为什么Comparable
使用Java ?为什么有人会Comparable
在课堂上实施?您需要实施可比较的现实生活示例是什么?
我有一个作业,我需要创建一个Student类,用于存储学生的neptun代码(String nep_c)和你在考试中获得的分数(int point_num).准备一个public int getMark()方法,根据下表将获得的票证作为分数的函数返回:
该类实现Comparable接口,compareTo()方法根据neptun代码对学生进行排序.在其中,通过您获得的标记数量.不幸的是我不懂comperTo方法.你能帮帮我怎样才能写出正确的代码?
public class Student implements Comparable{
private String nep_c;
private int point_num;
private int Mark;
public Student(int point_num, String nep_c) {
this.point_num = 65;
this.nep_c= "AAA1BB1";
}
public int getMark(){
if (point_num <= 100 && point_num > 90)
Mark = 5;
else if (point_num <= 90 && point_num > 80)
Mark = 4;
else if (point_num <= …
Run Code Online (Sandbox Code Playgroud) 我有一份清单List<Object[]>
.
Object []具有以下对象:
我可以在之后dd.MM.yyyy
和之后对列表进行排序String
.现在我想在时间之后对它进行排序,结果也应该排序为"dd.MM.yyyy",排序"字符串",排序"时间".
Collections.sort(resultList, new Comparator<Object[]>() {
public int compare(Object[] o1, Object[] o2) {
int dateComparison = (( o2[0]).compareTo(o1[0]));
return dateComparison == 0 ? ( o1[1]).compareTo( o2[1]) : dateComparison;
}
});
Run Code Online (Sandbox Code Playgroud)
我怎样才能让它发挥作用?
我正在使用PriorityQueue结构来获取用户设置的一些字段,这是代码的一部分:
package gqg;
import java.util.Queue;
public class Student {
//variables (ID, Name, ...), constructors, getters and setters...
Queue<Student> StudentQueue = new PriorityQueue<Student>();
public void Add() { //method to add the student at Queue
for(int x=0; x<1; x++) {
Student st = new Student();
System.out.println("+---------------------------+\n"
+ "| Students Registration |\n"
+ "+---------------------------+");
System.out.println("| Type the student's ID:");
stu.setID(user.nextInt());
System.out.println("| Type the student's name:");
stu.setName(user.next());
System.out.println("| Type the student's age:");
stu.setAge(user.nextInt());
//and other fields...
StudentQueue.add(st);
}
System.out.println("Done. Student has been added successfuly\n"); …
Run Code Online (Sandbox Code Playgroud) 我正在尝试在 PrimeFaces 数据表上实现排序功能。我创建了一个模型对象列表。表的排序顺序存在问题。排序列包含整数和字符串值。当我应用默认排序机制(例如列表)时sort=#{var.id}
,列表是基于 ASCII 顺序进行排序的。下图是排序结果的图片。我目前使用的是 PrimeFaces v5.2.5
<p:column headerText="Code Type" sortBy="#{var.codeType}">
<h:outputText value="#{var.codeType}"/>
</p:column>
Run Code Online (Sandbox Code Playgroud)
任何人都可以指导我如何克服这个问题。