ArrayList 的对象排序使用可比较的
ArrayList of objects sort using comparable
所以我正在处理地址簿作业,我一直坚持使用 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();
}
}
}
联系Class
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);
}
}
Any tips on why the contacts aren't sorting?
他们正在整理。但是你不会打印排序的 card
。
您重新阅读 readContact
中的联系人,然后打印它们,未排序。
可能你打算这样写:
if(option.equalsIgnoreCase("view")) {
System.out.println("\tADDRESS BOOK" + "\n" +
"=============================");
readContact();
Collections.sort(card);
printContacts();
}
并在 readContact
中更改此行:
ArrayList<Contact> card = (ArrayList<Contact>)is.readObject();
为此:
card = (ArrayList<Contact>)is.readObject();
并将打印从 readContact
移到它自己的方法中:
void printContacts() {
for(Contact temp : card) {
System.out.println(temp);
}
}
Second question, [...] any ideas how to get only the first char upper?
当然可以,使用这样的辅助方法:
private String toTitleCase(String name) {
return Character.toTitleCase(name.charAt(0)) + name.substring(1).toLowerCase();
}
您的问题如下:
此代码片段
Collections.sort(card);
con.getFullName();
readContact();
实际上是对你拥有的 card
集合进行排序,然后你调用 readContact()
方法在其中创建一个本地 card
集合,它隐藏了 card
集合你有在你的主程序中,并打印它的联系人,因为它们之前被写入文件。他们没有得到排序。
解决方案是这样的:
if(option.equalsIgnoreCase("view")) {
System.out.println("\tADDRESS BOOK" + "\n" +
"=============================");
con.getFullName(); // <------ ALSO, NOT QUITE SURE WHAT THIS IS FOR
readContact();
}
public void readContact() {
try (FileInputStream in = new FileInputStream("addressbook.txt")) {
ObjectInputStream is = new ObjectInputStream(in);
ArrayList<Contact> card = (ArrayList<Contact>)is.readObject();
Collections.sort(card); // <----------- THIS ADDED
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();
}
}
所以我正在处理地址簿作业,我一直坚持使用 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();
}
}
}
联系Class
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);
}
}
Any tips on why the contacts aren't sorting?
他们正在整理。但是你不会打印排序的 card
。
您重新阅读 readContact
中的联系人,然后打印它们,未排序。
可能你打算这样写:
if(option.equalsIgnoreCase("view")) {
System.out.println("\tADDRESS BOOK" + "\n" +
"=============================");
readContact();
Collections.sort(card);
printContacts();
}
并在 readContact
中更改此行:
ArrayList<Contact> card = (ArrayList<Contact>)is.readObject();
为此:
card = (ArrayList<Contact>)is.readObject();
并将打印从 readContact
移到它自己的方法中:
void printContacts() {
for(Contact temp : card) {
System.out.println(temp);
}
}
Second question, [...] any ideas how to get only the first char upper?
当然可以,使用这样的辅助方法:
private String toTitleCase(String name) {
return Character.toTitleCase(name.charAt(0)) + name.substring(1).toLowerCase();
}
您的问题如下: 此代码片段
Collections.sort(card);
con.getFullName();
readContact();
实际上是对你拥有的 card
集合进行排序,然后你调用 readContact()
方法在其中创建一个本地 card
集合,它隐藏了 card
集合你有在你的主程序中,并打印它的联系人,因为它们之前被写入文件。他们没有得到排序。
解决方案是这样的:
if(option.equalsIgnoreCase("view")) {
System.out.println("\tADDRESS BOOK" + "\n" +
"=============================");
con.getFullName(); // <------ ALSO, NOT QUITE SURE WHAT THIS IS FOR
readContact();
}
public void readContact() {
try (FileInputStream in = new FileInputStream("addressbook.txt")) {
ObjectInputStream is = new ObjectInputStream(in);
ArrayList<Contact> card = (ArrayList<Contact>)is.readObject();
Collections.sort(card); // <----------- THIS ADDED
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();
}
}