我已经给了一个分配,并且它已经是它的最后一天了。我做了大部分工作,但在最后一个问题上,我遇到了创建compareTo()函数的问题。
以下是我们想要的东西;
的compareTo
public int compareTo(java.lang.Object other)
Specified by:
compareTo in interface java.lang.Comparable
这就是我做的事情
public int compareTo(Object obj)
{
Document tmp = (Document)obj;
if(this.text < tmp.text)
{
/* instance lt received */
return -1;
}
else if(this.text > tmp.text)
{
/* instance gt received */
return 1;
}
/* instance == received */
return 0;
}
这是我的整个Document.java文件
类文档 { 私有字符串文本;
/**
* Default constructor; Initialize amount to zero.
*/
public Document()
{
text = "";
}
/**
* Default constructor; Initialize text to input value
* @param text New text value
*/
public Document(String text)
{
this.text = text;
}
/**
* Returns as a string the contents of the Document.
*/
public String toString()
{
return text;
}
这是自己的测试文件。
import java.util.Arrays;
public class Homework2扩展了文档{
/** ======================
* ContainsKeyword
* Returns true if the Document
* object passed in contains keyword as a substring
* of its text property.
* ======================
*/
public static boolean ContainsKeyword(Document docObject, String keyword)
{
if (docObject.toString().indexOf(keyword,0) >= 0)
return true;
return false;
}
public static void main(String[] args){
Email email1= new Email("Programming in Java",
"Larry", "Curly", "Programming");
Email email2 = new Email("Running marathons",
"Speedy", "Gonzales", "races");
System.out.println(email1);
File file1 = new File("Some Java file", "file.txt");
File file2 = new File(
"Boluspor wins against Besiktas. Muahahahaha",
"bolutas.txt");
Document doc = new Document (
"ok?,ok?,ok?,ok?,ok?,ok?,ok?,ok?,ok?,ok?,ok?,ok?");
System.out.println("\n"+file1);
System.out.println("\nWhich contains Java?");
if (ContainsKeyword(email1,"Java")) System.out.println(" Email1");
if (ContainsKeyword(email2,"Java")) System.out.println(" Email2");
if (ContainsKeyword(file1,"Java")) System.out.println(" File1");
if (ContainsKeyword(file2,"Java")) System.out.println(" File2");
Document [] da = new Document [5];
da[0] = email1;
da[1] = email2;
da[2] = file1;
da[3] = file2;
da[4] = doc;
Arrays.sort(da);
System.out.println("\nAfter sort:");
for(Document d : da){
System.out.println(d);
}
}
}
我想问的是,我无法比较Email.java和File.java中的对象,除了最后以Document [] da开头的部分之外,我还可以做任何事情......那部分会出错。我在这里做错了什么?
错误是;
Exception in thread "main" java.lang.ClassCastException: Email cannot be cast to java.lang.Comparable
at java.util.Arrays.mergeSort(Arrays.java:1144)
at java.util.Arrays.sort(Arrays.java:1079)
at Homework2.main(Homework2.java:53)
上传** 这是我的电子邮件和文件类..
/**
* First define class for Email, derive from Document
*/
class Email extends Document
{
private String sender;
private String recipient;
private String title;
private String body;
/**
* Constructors
*/
public Email()
{
super();
sender = "";
recipient = "";
title = "";
body = "";
}
public Email(String body, String sender, String recipient, String title)
{
this.sender = sender;
this.recipient = recipient;
this.title = title;
this.body = body;
}
// ======================
// Various accessor and mutator methods
// ======================
public String getSender()
{
return sender;
}
public void setSender(String sender)
{
this.sender = sender;
}
public String getRecipient()
{
return recipient;
}
public void setRecipient(String recipient)
{
this.recipient = recipient;
}
public String getTitle()
{
return title;
}
public void setTitle(String title)
{
this.title = title;
}
public String getBody(){
return body;
}
public void getBody(String body){
this.body = body;
}
/**
* Returns as a string the contents of the text fields concatenated
* together. Uses super.toString to get the parent's text.
*/
public String toString()
{
return "Sender:" + sender + ",Recipient:" + recipient + ",Title:" + title + ",Body:" + body + " " +
super.toString();
}
} // Email
和文件;
/**
* Next define class for File, derive from Document
* For brevity, short one-line methods are defined here in the
* header.
*/
class File extends Document
{
private String pathname;
/**
* Constructors.
*/
public File()
{
super();
pathname = "";
}
public File(String body, String pathname)
{
super(body);
this.pathname = pathname;
}
// ======================
// Various accessor and mutator methods
// ======================
public void setPathname(String s)
{
pathname = s;
}
public String getPathname()
{
return pathname;
}
/**
* Returns as a string the contents of the text fields concatenated
* together. Uses super.toString to get the parent's text.
*/
public String toString()
{
return "Pathname " + pathname + " Body " + super.toString();
}
} //文件
答案 0 :(得分:2)
你在哪里放compareTo
方法?如果您正在尝试对Document
的数组进行排序,则需要将Document实现为Comparable(或传入Comparator):
public class Document implements Comparable<Document> {
或者,如果由于某些奇怪的原因你不允许使用泛型:
public class Document implements Comparable {
然后将compareTo
放入Document
。
答案 1 :(得分:1)
失败的确切原因是因为您试图在未实现可比性的类上调用Arrays.sort
实施Comparable允许
calling Collections.sort and Collections.binarySearch calling Arrays.sort and Arrays.binarySearch using objects as keys in a TreeMap using objects as elements in a TreeSet
电子邮件未实现Comparable接口
使用
public class Email implements Comparable<Email>
请阅读此内容,帮助自己http://www.javapractices.com/topic/TopicAction.do?Id=10
另一个注意事项是你说要比较
Email.java和File.java
根据逻辑,你需要一个自定义函数。
compareTo用于比较同一类型的两个实例。这也意味着该功能存在于电子邮件类
中Email myEmail = new Email();
Email hisEmail = new Email();
myEmail.compareTo(hisEmail);
答案 2 :(得分:0)
错误信息中出现了错误。
您只能为实现Comparable的类对对象进行排序。你的班级没有。
在排序多种不同类型时,您可能希望提供自定义Comparator,或者使Document实现Comparable。
答案 3 :(得分:0)
试试这个:
这是我的整个Document.java文件
class Document implements Comparable { //this line is your solution.
private String text;
/**
* Default constructor; Initialize amount to zero.
*/
public Document()
{
text = "";
}
/**
* Default constructor; Initialize text to input value
* @param text New text value
*/
....}