对象的ArrayList使用可比较的

时间:2015-10-18 07:16:46

标签: java sorting arraylist comparable

所以我正在处理通讯录任务,而且我仍然坚持要通过姓氏对联系人进行排序。我尝试的东西,我们还没有像ArrayLists那样学习对象,可比较和可序列化和可比性让我最困惑。

关于联系人为什么不排序的任何提示?第二个问题,我想尝试将名字和姓氏的第一个字符设为大写字母,但我无法弄明白,所以我在toString方法中将整个事物设为大写,任何想法如何只得到第一个char上层?

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);
}

}

2 个答案:

答案 0 :(得分:2)

您的问题如下: 此代码段

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();
    }
}

答案 1 :(得分:1)

  

关于联系人为何没有排序的任何提示?

他们正在排序。但是你不打印已排序的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);
    }
}
  

第二个问题,[...]任何想法如何只获得第一个char上层?

当然,使用这样的辅助方法:

private String toTitleCase(String name) {
    return Character.toTitleCase(name.charAt(0)) + name.substring(1).toLowerCase();
}