无法弄清楚如何从扩展类中对 arrayList 进行排序...
这是我所拥有的:
通讯录类:
import java.util.ArrayList;
import java.util.Iterator;
public class AddressBook
{
private ArrayList<Entry> data;
public AddressBook()
{
this.data = new ArrayList();
}
public String add(Entry paramEntry)
{
//Some code }
public ArrayList<Entry> getAddressBook()
{
return this.data;
}
public String getAll()
{
//Some code
}
}
扩展地址簿类:
import java.util.*;
import java.lang.Comparable;
public class ExtendedAddressBook extends AddressBook
{
public ExtendedAddressBook()
{
}
public String getAll()
{
String listAll = "Address Book\n";
ArrayList<Entry> allEntries = getAddressBook();
Collections.sort(allEntries);
for ( Entry entry : allEntries )
{
ListAll = ListAll + entry.toString();
}
return ListAll;
}
}
所有条目类:
import java.lang.Comparable;
public class AllEntries extends Entry implements Comparable<Entry>
{
public int compareTo(Entry otherEntry)
{
int d = this.getFirstName().compareTo(otherEntry.getFirstName());
if (d == 0)
{
d = this.getLastName().compareTo(otherEntry.getLastName());
}
return d;
}
}
当我尝试编译时,我在以下行 Collections.sort(allEntries); 在 ExtendedAddressBook 类中。有人可以指出我哪里出错了,如果它甚至可以扩展一个类以便可以对 arrayList 进行排序?谢谢