【发布时间】:2016-01-16 16:09:56
【问题描述】:
所以我正在处理通讯簿分配,但我一直坚持使用 Comparable 按姓氏对联系人进行排序。我正在尝试我们还没有真正学到的东西,比如对象的 ArrayLists、可比较和可序列化和可比较最让我困惑。
关于为什么联系人没有排序的任何提示?第二个问题,我想尝试将名字和姓氏的第一个字符设为大写,但我无法弄清楚,所以我在 toString 方法中将整个内容设为大写,任何想法如何只获取第一个字符上?
public class AddressBook implements Serializable{
private ArrayList<String> newBook = new ArrayList<String>();
private String dataFile;
private ArrayList<Contact> card =new ArrayList<Contact>(50);
private Contact[] contacts;
private int size = 0;
private int capacity = 0;
private String firstName;
private String lastName;
public static void main(String[] args) {
AddressBook AB = new AddressBook();
AB.addressBookMenu();
}
public void addressBookMenu() {
Scanner scan = new Scanner(System.in);
String option = "";
System.out.println("PLEASE SELECT ONE OF THE FOLLOWING OPTIONS: ");
System.out.println("\t add --> Add a new contact ");
System.out.println("\t find --> Find a contact ");
System.out.println("\t edit --> Edit an existing contact ");
System.out.println("\t view --> View the current address book");
System.out.println("\t save --> Save the current address book");
System.out.println("\t quit --> quit");
System.out.println();
option = scan.nextLine();
while(!(option.equalsIgnoreCase("quit"))) {
Contact con = new Contact(firstName, lastName);
if(option.equalsIgnoreCase("add")) {
System.out.println("Enter First Name: ");
String tempFirst = scan.nextLine();
System.out.println("Enter Last Name: ");
String tempLast = scan.nextLine();
con.setFirstName(tempFirst);
con.setLastName(tempLast);
card.add(con);
writeContact();
}
//View address book
if(option.equalsIgnoreCase("view")) {
System.out.println("\tADDRESS BOOK" + "\n" +
"=============================");
Collections.sort(card);
con.getFullName();
readContact();
}
System.out.println();
System.out.println("PLEASE SELECT ONE OF THE FOLLOWING OPTIONS: ");
System.out.println("\t add --> Add a new contact ");
System.out.println("\t find --> Find a contact ");
System.out.println("\t edit --> Edit an existing contact ");
System.out.println("\t view --> View the current address book");
System.out.println("\t save --> Save the current address book");
System.out.println("\t quit --> quit");
System.out.println();
option = scan.nextLine();
}
}
public void writeContact() {
try (FileOutputStream out = new FileOutputStream("addressbook.txt")) {
ObjectOutputStream os = new ObjectOutputStream(out);
os.writeObject(card);
os.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void readContact() {
try (FileInputStream in = new FileInputStream("addressbook.txt")) {
ObjectInputStream is = new ObjectInputStream(in);
ArrayList<Contact> card = (ArrayList<Contact>)is.readObject();
for(Contact temp : card) {
System.out.println(temp);
}
is.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
联系类
public class Contact implements Comparable<Contact>, Serializable{
private String firstName;
private String lastName;
private String email;
private String phone;
public Contact() {
firstName = "";
lastName = "";
}
public Contact(String ln, String fn) {
lastName = ln;
firstName = fn;
}
public void setFirstName(String fn) {
firstName = fn;
}
public void setLastName(String ln) {
lastName = ln;
}
public void setFullName(String fn, String ln) {
firstName = fn;
lastName = ln;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public String getFullName() {
return lastName + firstName;
}
public String toString() {
return
"FIRST NAME: " + getFirstName().substring(0).toUpperCase() + "\t" +
"LAST NAME: " + getLastName().substring(0).toUpperCase() + "\n";
}
@Override
public int compareTo(Contact nextContact) {
return lastName.compareTo(nextContact.lastName);
}
}
【问题讨论】:
-
请在每个帖子中只问 一个 问题,最好使用 简短 但完整的程序来演示问题。我怀疑问题是您的
readContact()忽略了您刚刚排序的集合的内容... -
我需要在 readContent 方法中对其进行排序吗?
-
stackoverflow.com/questions/1892765/… 回答您关于首字母大写的问题
-
好吧,您当然不想在阅读时打印,除非联系人在写入之前进行了排序...您应该真正调试您的代码以了解发生了什么。
-
谢谢 Jens 我会检查一下
标签: java sorting arraylist comparable