我收到一條符合theTree.insert(new File("a", 1));
的錯誤:The method insert(Comparable) in the type Tree is not applicable for the arguments (File)
。爲什麼我不能將我的File對象插入爲可比較的?
而當我嘗試拋出這樣的theTree.insert((Comparable) new File("a", 1));
我得到其他錯誤:E xception in thread "main" java.lang.ClassCastException: File cannot be cast to java.lang.Comparable at TreeApp.main(tree.java:134)
。
爲什麼我不能傳遞這個對象?
我的目標:
class FileObject
{
public String name;
public int size;
File(String a, int b)
{
this.name = a;
this.size = b;
}
}
java.io.File是否實現了Comparable?我猜不是,你如何判斷兩個文件是否相等?通過fileno?按文件名?還是按內容?我將擴展實現Comparable的File類(從而決定compareTo()行爲應該是什麼。(對不起,我假定File是標準的File類,但推理也適用於你的類) – BigMike