使用 compareTo() 比较 Object 的值
Comparing an Object's values with compareTo()
我正在使用 Max-Heap 来存储 object 类型 Song
。一首歌曲有标题和评级,如 Song
class 中所示。我希望 Song
object 按评分进行比较,以便首先显示评分最高的歌曲。如果歌曲的评级相同,则应按标题的字母顺序进行比较。我现在拥有的是目前以最高评级输出它,但不正确。
堆:
public class Heap<T extends Comparable<T>> {
private ArrayList<T> heap;
public Heap(){
heap = new ArrayList<T>();
}
public int getPLoc(int i){
return (i - 1) / 2;
}
public int getLCLoc(int i){
return 2 * i + 1;
}
public int getRCLoc(int i){
return 2 * i + 2;
}
public T getNodeAt(int i) {
if(heap.get(i) == null) {
System.out.println("Item does not exist.");
return null;
}else {
return heap.get(i);
}
}
public void addNode(T n) {
heap.add(null);
int index = heap.size() - 1;
while(index > 0 && (getNodeAt(getPLoc(index)).compareTo(n)) < 0) { //Is this correct?
heap.set(index, getNodeAt(getPLoc(index)));
index = getPLoc(index);
}
heap.set(index, n);
}
歌曲:
public class Song implements Comparable<Song> {
private String title;
private String rating;
public Song(String t, String r) {
title = t;
rating = r;
}
public String getTitle(){
return title;
}
public String getRating(){
return rating;
}
// Need help here adding it to also compare by alphabetical title if songs have same ratings.
public int compareTo(Song s) {
return rating.compareTo(s.getRating());
}
解决方案是比较歌曲的名称,如果它们的排名相等(并且 compareTo 因此 returns 0)和 return 第二次比较的结果
compareTo() 方法 returns 具有以下值的 int
:
负数如果thisObject < anotherObject
零如果thisObject == anotherObject
阳性如果thisObject > anotherObject
Check for value zero
,意思是等级一样,那就去title comparison
。
示例代码,可以调整
public int compareTo(Song s) {
int val = rating.compareTo(s.getRating());
if(val == 0){
val = title.compareTo(s.getTitle());
}
return val;
}
我正在使用 Max-Heap 来存储 object 类型 Song
。一首歌曲有标题和评级,如 Song
class 中所示。我希望 Song
object 按评分进行比较,以便首先显示评分最高的歌曲。如果歌曲的评级相同,则应按标题的字母顺序进行比较。我现在拥有的是目前以最高评级输出它,但不正确。
堆:
public class Heap<T extends Comparable<T>> {
private ArrayList<T> heap;
public Heap(){
heap = new ArrayList<T>();
}
public int getPLoc(int i){
return (i - 1) / 2;
}
public int getLCLoc(int i){
return 2 * i + 1;
}
public int getRCLoc(int i){
return 2 * i + 2;
}
public T getNodeAt(int i) {
if(heap.get(i) == null) {
System.out.println("Item does not exist.");
return null;
}else {
return heap.get(i);
}
}
public void addNode(T n) {
heap.add(null);
int index = heap.size() - 1;
while(index > 0 && (getNodeAt(getPLoc(index)).compareTo(n)) < 0) { //Is this correct?
heap.set(index, getNodeAt(getPLoc(index)));
index = getPLoc(index);
}
heap.set(index, n);
}
歌曲:
public class Song implements Comparable<Song> {
private String title;
private String rating;
public Song(String t, String r) {
title = t;
rating = r;
}
public String getTitle(){
return title;
}
public String getRating(){
return rating;
}
// Need help here adding it to also compare by alphabetical title if songs have same ratings.
public int compareTo(Song s) {
return rating.compareTo(s.getRating());
}
解决方案是比较歌曲的名称,如果它们的排名相等(并且 compareTo 因此 returns 0)和 return 第二次比较的结果
compareTo() 方法 returns 具有以下值的 int
:
负数如果thisObject < anotherObject
零如果thisObject == anotherObject
阳性如果thisObject > anotherObject
Check for value zero
,意思是等级一样,那就去title comparison
。
示例代码,可以调整
public int compareTo(Song s) {
int val = rating.compareTo(s.getRating());
if(val == 0){
val = title.compareTo(s.getTitle());
}
return val;
}