Java 对象的ArrayList使用comparable排序

Java 对象的ArrayList使用comparable排序,java,sorting,arraylist,comparable,Java,Sorting,Arraylist,Comparable,所以我正在做一个通讯录作业,我一直在按姓氏对联系人进行排序。我正在尝试一些我们还没有真正学会的东西,比如对象的数组列表,可比较的,可序列化的,可比较的,最让我困惑的是 有没有关于为什么联系人没有分类的提示?第二个问题,我想尝试将名字和姓氏的第一个字符设为大写,但我就是想不出来,所以我在toString方法中将整个字符设为大写,有没有办法只将第一个字符设为大写 public class AddressBook implements Serializable{ private ArrayList&

所以我正在做一个通讯录作业,我一直在按姓氏对联系人进行排序。我正在尝试一些我们还没有真正学会的东西,比如对象的数组列表,可比较的,可序列化的,可比较的,最让我困惑的是

有没有关于为什么联系人没有分类的提示?第二个问题,我想尝试将名字和姓氏的第一个字符设为大写,但我就是想不出来,所以我在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();
    }
}
}
公共类AddressBook实现可序列化{
private ArrayList newBook=new ArrayList();
私有字符串数据文件;
私有ArrayList卡=新ArrayList(50);
私人联系人[]联系人;
私有整数大小=0;
专用int容量=0;
私有字符串名;
私有字符串lastName;
公共静态void main(字符串[]args){
地址簿AB=新地址簿();
AB.addressBookMenu();
}
public void addressBookMenu(){
扫描仪扫描=新扫描仪(System.in);
字符串选项=”;
System.out.println(“请选择以下选项之一:”);
System.out.println(“\t添加-->添加新联系人”);
System.out.println(“\t查找-->查找联系人”);
System.out.println(“\t编辑-->编辑现有联系人”);
System.out.println(“\t查看-->查看当前通讯簿”);
System.out.println(“\t保存-->保存当前通讯簿”);
System.out.println(“\t退出-->退出”);
System.out.println();
option=scan.nextLine();
而(!(option.equalsIgnoreCase(“退出”)){
联系人con=新联系人(姓、名);
if(option.equalsIgnoreCase(“添加”)){
System.out.println(“输入名字:”);
字符串tempFirst=scan.nextLine();
System.out.println(“输入姓氏:”);
String tempLast=scan.nextLine();
con.setFirstName(tempFirst);
con.setLastName(tempLast);
卡片。添加(con);
writeContact();
}   
//查看通讯簿
if(option.equalsIgnoreCase(“视图”)){
System.out.println(“\t服装手册”+“\n”+
"=============================");
收款.分类(卡片);
con.getFullName();
readContact();
}
System.out.println();
System.out.println(“请选择以下选项之一:”);
System.out.println(“\t添加-->添加新联系人”);
System.out.println(“\t查找-->查找联系人”);
System.out.println(“\t编辑-->编辑现有联系人”);
System.out.println(“\t查看-->查看当前通讯簿”);
System.out.println(“\t保存-->保存当前通讯簿”);
System.out.println(“\t退出-->退出”);
System.out.println();
option=scan.nextLine();
}
}
公共无效writeContact(){
try(FileOutputStream out=newfileoutputstream(“addressbook.txt”){
ObjectOutputStream os=新的ObjectOutputStream(输出);
os.writeObject(卡);
os.close();
}catch(filenotfounde异常){
e、 printStackTrace();
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}
public-void-readContact(){
try(FileInputStream in=newfileinputstream(“addressbook.txt”)){
ObjectInputStream is=新的ObjectInputStream(in);
ArrayList卡片=(ArrayList)为.readObject();
用于(联系人:卡){
系统输出打印项次(温度);
}
is.close();
}catch(filenotfounde异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}catch(classnotfounde异常){
//TODO自动生成的捕捉块
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);
}

}
public类Contact实现了可比较的、可序列化的{
私有字符串名;
私有字符串lastName;
私人字符串电子邮件;
私人电话;
公众联络(){
firstName=“”;
lastName=“”;
}
公共联系人(字符串ln,字符串fn){
lastName=ln;
firstName=fn;
}
public void setFirstName(字符串fn){
firstName=fn;
}
public void setLastName(字符串项次){
lastName=ln;
}
public void setFullName(字符串fn,字符串ln){
firstName=fn;
lastName=ln;
}
公共字符串getFirstName(){
返回名字;
}
公共字符串getLastName(){
返回姓氏;
}
公共字符串getFullName(){
返回lastName+firstName;
}
公共字符串toString(){
返回
名:“+getFirstName().substring(0).toUpperCase()+”\t”+
姓氏:“+getLastName().substring(0.toUpperCase()+”\n”;
}
@凌驾
公共int比较(联系下一个联系人){
返回lastName.compareTo(nextContact.lastName);
}
}
有没有关于为什么联系人没有分类的提示

他们正在分类。但是,您不打印已排序的
。 您可以在
readContact
中重新读取联系人,然后打印,不排序

也许你是想这样写的:

if(option.equalsIgnoreCase("view")) {
    System.out.println("\tADDRESS BOOK" + "\n" +
            "=============================");

    readContact();
    Collections.sort(card);
    printContacts();
}
并在
readContact
中更改此行:


第二个问题,[…]有没有办法只得到第一个字符

当然,使用以下帮助器方法:

private String toTitleCase(String name) {
    return Character.toTitleCase(name.charAt(0)) + name.substring(1).toLowerCase();
}
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();
    }
}

你的问题如下: 此代码段

Collections.sort(card);
con.getFullName();
readContact();
实际上是对您拥有的
集合进行排序,然后调用
readContact()
方法,该方法在其中创建一个本地
集合,该集合对主程序中的
集合进行阴影处理,并打印其联系人,就像之前将其写入文件一样。他们没有分类

解决方案如下:

private String toTitleCase(String name) {
    return Character.toTitleCase(name.charAt(0)) + name.substring(1).toLowerCase();
}
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();
    }
}
if(option.equalsIgnoreCase(“视图”))
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();
    }
}