所以我一直在试图弄清楚Collections如何在Java中工作(通过查看我们在uni中做的一个旧项目),但是我一直遇到一些我无法解决的错误,它与Comparable函数有关
这是我的第一堂课:
import java.io.*;
import java.util.*;
public class Client {
private int hour;
private int minute;
private int min_per_stay;
private double price;
public Client(int h, int m, int mps, double p)
{
this.hour = h;
this.minute = m;
this.min_per_stay = mps;
this.price = p;
}
public void SetHour(int h)
{
this.hour = h;
}
public void SetMin(int m)
{
this.minute = m;
}
public void SetMinPStay(int mps)
{
this.min_per_stay = mps;
}
public void SetPrice(double p)
{
this.price = p;
}
public int GetHour()
{
return hour;
}
public int GetMin()
{
return minute;
}
public int GetMinPStay()
{
return min_per_stay;
}
public double GetPrice()
{
return price;
}
public String toString()
{
return "Hours: " + this.hour +
" Minutes: " + this.minute +
" Minutes per stay:" + this.min_per_stay +
" Price: " + this.price + "\n";
}
public boolean equals(Client c) {
return (this.hour == ((Client)c).GetHour() && this.minute == ((Client)c).GetMin());
}
public int CompareTo(Client c)
{
if (this.hour<((Client)c).hour) return -1;
if(this.minute<((Client)c).minute) return -1;
if (this.hour>((Client)c).hour) return 1;
if(this.minute>((Client)c).minute) return 1;
return 0;
}
}
我的收藏:
import java.io.*;
import java.util.*;
public class ClientCollection{
private SortedSet<Client> oClient = new TreeSet<Client>();
public ClientCollection()
{
}
public ClientCollection(String FileName)
{
try
{
Scanner sc = new Scanner(new File(FileName));
while(sc.hasNextLine())
{
oClient.add(new Client(sc.nextInt(),sc.nextInt(),
sc.nextInt(),sc.nextDouble()));
}
sc.close();
}
catch (FileNotFoundException e)
{
System.out.println("File not found!");
}
}
public void addClient(Client c)
{
oClient.add(c);
}
public List<Client> reverseList()
{
List<Client> oList1 = new ArrayList<Client>(oClient);
List<Client> oList2 = new ArrayList<Client>();
for(ListIterator<Client> it = (oList1.listIterator(oClient.size())); it.hasPrevious(); )
{
Client res = it.previous();
oList2.add(res);
}
return oList2;
}
public void printColl()
{
System.out.println(oClient.toString());
}
public static void main(String[] args)
{
ClientCollection oc = new ClientCollection("test.txt");
oc.printColl();
oc.reverseList();
oc.printColl();
}
}
我得到的错误是:
Exception in thread "main" java.lang.ClassCastException: Client cannot be cast to java.base/java.lang.Comparable
at java.base/java.util.TreeMap.compare(Unknown Source)
at java.base/java.util.TreeMap.put(Unknown Source)
at java.base/java.util.TreeSet.add(Unknown Source)
at ClientCollection.<init>(ClientCollection.java:20)
at ClientCollection.main(ClientCollection.java:56)
如果有人向我解释错误,我会非常高兴,我仍然有点新鲜:(
答案 0 :(得分:2)
您的inds <- do.call(intersect, unname(by(num, num$x5, function(x) x$x4)))
num[num$x4 %in% inds, ]
x1 x2 x3 x4 x5
2 74 96 36 2 2017
3 61 83 7 3 2017
4 63 35 33 2 2018
5 44 95 67 3 2018
课程没有实现Client
界面(即Comparable<Client>
没有自然排序)。
因此,为了将其用作Client
的元素,您必须将TreeSet
构造函数传递给TreeSet<Client>
,它指定Comparator<Client>
的顺序} elements。
如果不这样做会导致Client
,因为ClassCastException
类(或者更确切地说是在幕后使用的TreeSet
类)假定如果你没有提供{ {1}}在构造函数中,这意味着您的TreeMap
实现了Comparator
。
编辑:由于Client
课程中有Comparable
方法,因此您似乎打算实施CompareTo
。
更改:
Client
为:
Comparable
并改变:
public class Client
为:
public class Client implements Comparable<Client>
您可能希望修改public int CompareTo(Client c)
方法的逻辑。
答案 1 :(得分:0)
除了@Eran写的内容之外,您还需要编写以小写字母开头的方法名称。您可以看到StackOverflow的格式化程序被它弄糊涂了。它也会让任何人不得不混淆你的代码。
您的compareTo
方法应如下所示:
public int compareTo(final Client c)
{
final int diffHours = this.hour - c.hour;
if (diffHours == 0) {
return this.minute - c.minute;
} else {
return diffHours;
}
}
转换是不必要的,因为c
参数已经是Client
类型。