JAVA:修改,删除,列出一个arraylist地址簿

时间:2015-05-13 02:14:03

标签: java arraylist

善待,我正在学习。

我正在创建一个地址簿,我需要添加名称才能进入arraylist。我遇到的问题是在arraylist中修改,删除和列出所有内容。我该怎么做?

这是我的班级文件:

import java.text.SimpleDateFormat;
import java.text.DecimalFormat;
import java.util.*;

public class AddressBook {

    private static int totalNumber;

    public static int getTotal() {

        //Returns total number of employees
        return totalNumber;
    }

    private Date lastModified;
    private String fullname;
    private String address;
    private String city;
    private String state;
    private String zip;
    private String phone;


    public AddressBook() {
        super();
    }

    /*public AddressBook(Date lastModified, String fullname, String address, String city, String state, String zip, String phone, int month, int day, int year) { 
        this.fullname = fullname;
        this.address = address;
        this.city = city;
        this.state = state;
        this.zip = zip;
        this.phone = phone; 
        GregorianCalendar calendar = new GregorianCalendar(year, month-1, day );
        this.lastModified = calendar.getTime();

    }*/
    public AddressBook(String fullname, String address, String city, String state, String zip, String phone) { 
        this.fullname = fullname;
        this.address = address;
        this.city = city;
        this.state = state;
        this.zip = zip;
        this.phone = phone; 


    }


    public Date getLastModified() {

        return lastModified;
    }

    public void setLastModified(int month, int day, int year) {
        month = 00; 
        day = 00;
        year = 0000;
        return;
    }
    public String getFullname() {
        return fullname;
    }
    public void setFullname(String fullname) {
        this.fullname = fullname;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
    public String getCity() {
        return city;
    }
    public void setCity(String city) {
        this.city = city;
    }
    public String getState() {
        return state;
    }
    public void setState(String state) {
        this.state = state;
    }
    public String getZip() {
        return zip;
    }
    public void setZip(String zip) {
        this.zip = zip;
    }
    public String getPhone() {
        return phone;
    }
    public void setPhone(String phone) {
        this.phone = phone;
    }


    @Override
    public String toString() {
        return "Employee [name=" + this.fullname + ", Address=" + this.address + ", City="
                + this.city + ", state=" + this.state + ", zip=" + this.zip
                + ", phone=" + this.phone + "]";
    }



}

这是测试文件:

import java.util.*;

public class testAddressBook {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner sc = new Scanner(System.in);

        boolean switcher = true;
        do {
        System.out.println("\n\tAddress Book Menu");
        System.out.println("\n\t\tEnter A to (A)dd Person ");
        System.out.println("\t\tEnter D to (D)elete Person");
        System.out.println("\t\tEnter M to (M)odify Person");
        System.out.println("\t\tEnter S to (S)earch Address Book ");
        System.out.println("\t\tEnter L to (L)ist ALL (sorted) ");
        System.out.println("\t\tEnter Q to Quit");
        System.out.print("\n\tPlease enter your choice: ");
        char choice = sc.nextLine().toUpperCase().charAt(0);


        while ((choice != 'A') && (choice != 'D') && (choice != 'M')  && (choice != 'S') && (choice != 'L')&& (choice != 'Q')) {
            System.out.println("Invalid choice!  Please select (A)dd, (D)elete, (M)odify, (S)earch, (L)ist or (Q)uit: ");
            choice = sc.nextLine().toUpperCase().charAt(0);
        }


        AddressBook newPerson = new AddressBook();
        ArrayList<AddressBook> person = new ArrayList<>(); 

        switch (choice) {
        case 'A' :      
        System.out.println("\nTo add a person, follow the prompts.");

        System.out.print("\nEnter Fullname: ");
        newPerson.setFullname(sc.nextLine());

        System.out.print("Enter Address: ");
        newPerson.setAddress(sc.nextLine());

        System.out.print("Enter City: ");
        newPerson.setCity(sc.nextLine());

        System.out.print("Enter State: ");
        newPerson.setState(sc.nextLine());

        System.out.print("Enter Zip: ");
        newPerson.setZip(sc.nextLine());

        System.out.print("Enter Phone Number: ");
        newPerson.setPhone(sc.nextLine());



        person.add(new AddressBook(newPerson.getFullname(), newPerson.getAddress(), newPerson.getCity(), newPerson.getState(), newPerson.getZip(), newPerson.getPhone()));
        System.out.println(person.get(0));


        System.out.println("\nYou have successfully added a new person!");

        break;

        case 'D' :



        break;
        case 'M' :



            break;
        case 'S' :




            break;
        case 'L' :








                                break;
        case 'Q' :
            switcher = false;
            System.exit(0);
            break;
        default:

        }

    }
        while (switcher != false);

    }}

2 个答案:

答案 0 :(得分:1)

这是您的ArrayList java doc。

以下是for-loop

的一些信息

看看。

get()

add()

remove()

person.get(0)实际上是您的AddressBook对象。您将能够使用

修改该条目

希望这能提供足够的提示让你入门。

初学者提示:请记住保持步伐小。一旦您能够创建单个条目,请修改,然后将其删除。做两个。注意重复的代码......然后使用循环

对其进行编程

到目前为止您遇到的一些错误:

    AddressBook newPerson = new AddressBook();
    ArrayList<AddressBook> person = new ArrayList<>(); 
    // ... adding values to your newPerson
    person.add(new AddressBook(newPerson.getFullname(),           newPerson.getAddress(), newPerson.getCity(), newPerson.getState(), newPerson.getZip(), newPerson.getPhone()));

你正在填充你的人。然后创建一个具有相同人口值的新人。你可以简单地将原来的人传递给arraylist

    person.add(newPerson);

答案 1 :(得分:0)

您有多个建议

  1. 使用有意义的名称。例如,ArrayList<AddressBook>应该已命名为listOfAddressBook等。
  2. 您可以直接将AddressBook对象添加到列表中:我的意思是: person.add(newPerson)而不是person.add(new AddressBook(newPerson.getFullname(), newPerson.getAddress(), newPerson.getCity(), newPerson.getState(), newPerson.getZip(), newPerson.getPhone()));并且再次出现混淆,即对象属于AddressBook类型,但名称为newPerson
  3. 有一些方法可以移除项目或修改ArrayList中的项目,最好阅读它们以改善您的学习,而不是寻求帮助。我鼓励你在JLS中阅读它们。
  4. 您还可以将列表声明为: List<AddressBook> person = new ArrayList<>();所以去找出为什么这比你的方法更好。