Arraylist中的无限对象(Java)

时间:2015-03-21 20:42:24

标签: java arraylist

我正在开展一个学校项目。 我们必须创建联系人列表。

所以,我创建了一个生成像。

这样的对象的类
    package coursework_q2;


public class Agenda {
    private String name;
    private long phoneNumber;
    private String email;
    private String type;

    public Agenda(){
        name = "";
        phoneNumber = 0;
        email = "";
        type = "";
    }//end of constructor

    @Override
public String toString() {
    return "\nName: " + this.getName() + 
           ", Number: " + this.getPhoneNumber() +
           ", Email: " + this.getEmail() +
           ", Type: " + this.getType();
}



    //setters
    public void setName(String n){
        name = n;
    }//end of setName

    public void setPhoneNumber(long n){
        phoneNumber = n;
    }//end of setPhoneNumber

    public void setEmail(String e){
        email = e;
    }

    public void setType(int t){
        if (t==1){
            type = "Personal";  
        }else{
            type = "Business";
        }
    }//end of setType

    //getters
    public String getName(){
        return name;
    }//end of getName

    public long getPhoneNumber(){
        return phoneNumber;
    }//end of getPhoneNumber

    public String getEmail(){
        return email;
    }//end of getEmail

    public String getType(){
        return type;
    }//end of getType

}//end of class

在我的其他课程中,我将它们存储在arraylist

我的问题是我不知道如何创建用户想要的联系人。

例如:

package testcourse;

import java.util.ArrayList; import java.util.Scanner;

公共课程CourseWork_Q2 {

public static void main(String[] args) {
    ArrayList<Agenda> contacts = new ArrayList();
    Scanner in = new Scanner(System.in);
    int check;
    String contact;
    String search;
    Agenda a;
    a = new Agenda();



    int select=0;

    int b=0;

    do {

        System.out.println("1. Add Contact\n2. Edit Contact\n3. Delete Contact\n4. Display All Contacts\n5. Quit");
        System.out.print("Please use the numbers 1-5 to choose an option: ");

        do{//input validation
            check=0;
            if(in.hasNextInt()){
                select = in.nextInt();
                if(1<=select && 5>=select){
                    check=1;
                }else{
                    System.out.println("Invalid Input!\nPlease use the numbers 1-5 to choose an option: ");
                    System.out.println("1. Add Contact\n2. Edit Contact\n3. Delete Contact\n4. Display All Contacts\n5. Quit");
                }//end of if
            }else{
                System.out.println("Invalid Input!\nPlease use the numbers 1-5 to choose an option: ");
                System.out.println("1. Add Contact\n2. Edit Contact\n3. Delete Contact\n4. Display All Contacts\n5. Quit");
                in.next();
            }//end of if
        }while(check==0);


        switch(select){
            case 1: System.out.println("Add Contact");                                                

                    System.out.print("Please enter your contact name: ");
                    a.setName(in.next());
                    System.out.print("\nPlease enter your contact phone-number: ");
                    a.setPhoneNumber(in.nextLong());
                    System.out.print("\nPlease enter your contact email: ");
                    a.setEmail(in.next());
                    System.out.print("\nPlease choose your contact type: ");
                    System.out.println("\n1. Personal\n2. Business");
                    a.setType(in.nextInt());

                    contacts.add(a);
                break;
            case 2: System.out.println("Edit Contact");
                    System.out.println("Please enter the name of the contact you wish to edit: ");
                    search = in.next();

                    for(int i=0; i<contacts.size(); i++){
                            contact = (contacts.get(i)).toString();


                        if(contact.contains("Name: "+search)){
                            System.out.println(contacts.get(i));
                            System.out.println("Please edit the name");
                            a.setName(in.next());
                        }
                    }
                break;
            case 3: System.out.println("Delete Contact");

                break;
            case 4: System.out.println("Display All Contact");
                    for (int i=0; i<contacts.size(); i++){
                    System.out.println(contacts.get(i));
                    }
                break;
            case 5:
                System.exit(5);
                break;
        }//end of switch


    }while (!(check==5));




}//end of main

} //班级的结尾

依旧......

我如何使用我的代码自动执行此操作?那么用户可以根据需要创建尽可能多的联系人吗?

谢谢!

1 个答案:

答案 0 :(得分:3)

创建ArrayList议程:

ArrayList<Agenda> contacts = new ArrayList<Agenda>();

每次要添加联系人时,请创建新的Agenda,设置值,然后将其添加到ArrayList

Agenda a = new Agenda();
// Do stuff to the agenda/contact, like set names and stuff
contacts.add(a); // Adds the agenda/contact to the ArrayList

要编辑联系人,您需要做的就是从ArrayList中检索它,并在数组中找到它的索引。

int index = 1; // Set it to whichever you need
Agenda a = contacts.get(index);
// Now change whatever is in the agenda

要删除联系人,只需使用索引将其从ArrayList中删除。

int index = 5;
contacts.remove(index);