简体   繁体   English

在对象 Java 中访问 ArrayList

[英]Accessing ArrayList in Object Java

I am making a contact book, it comprises of contacts in an ArrayList saved in the contact object, but I cannot think of a way to access the contacts in the Addressbook, can someone help me?我正在制作通讯录,它包含保存在联系人对象中的 ArrayList 中的联系人,但我想不出访问通讯录中联系人的方法,有人可以帮助我吗? a snippet of my code below:下面是我的代码片段:

//structure of each contact
public class Contact {

private String name;
private String cellNo;
private String email;
private String category;

}

//structure of the address book
public class AddressBook {


private ArrayList <Contact> contacts = new ArrayList<Contact>(); 

}


//Full AddressBook Class

 import java.util.ArrayList;


  public class AddressBook {


  private ArrayList <Contact> contacts = new ArrayList<Contact>(); 


  public AddressBook(ArrayList <Contact> contacts)
  {
    this.contacts = contacts;


  public ArrayList<Contact> getContact() {
    return contacts;
  }

  public void setContact(ArrayList<Contact> contacts) {
    this.contacts = contacts;
  }



    public boolean addContact (Contact contact)
  {
        if(contact != null)
        {
        try{
        contacts.add(contact);
        return true;
        }catch(NullPointerException e)
        {
            return false;
        }
        }
    return false;
   }
    public boolean deleteContact(int index)
   {
    if(contacts != null)
    {
        contacts.remove(index);
        return true;

    }else {

    }
    return false;
}

public void viewSpecificContact(String number)
{
    System.out.print("Search Results");
    for(int i = 0; i < contacts.size(); i++)
    {

        if(contacts.get(i).getCellNo() == number)
        {
            System.out.println(contacts.get(i).getName());
            System.out.println(contacts.get(i).getCellNo());
            System.out.println(contacts.get(i).getEmail());
            System.out.println(contacts.get(i).getCategory());

            System.out.println("Contact Found");

    }else{
        System.out.println("Contact Not Found");
          }
}


}



 }


 //full contact class

 public class Contact {

private String name;
private String cellNo;
private String email;
private String category;



 //Constructor  
public Contact(String name, String cellNo, String email, String category)
{
    this.name = name;
    this.cellNo = cellNo;
    this.email = email;
    this.category = category;
}


 //Setters and Getters
public void setName(String name) {
    this.name = name;
}

public String getName() {
    return name;
}


public String getCellNo() {
    return cellNo;
}


public void setCellNo(String cellNo) {
    this.cellNo = cellNo;
}


public String getEmail() {
    return email;
}


public void setEmail(String email) {
    this.email = email;
}


public String getCategory() {
    return category;
}


public void setCategory(String category) {
    this.category = category;
}


  //Printing Area   
public void printName ()
{
    System.out.print(getName());
}

public void printEmail()
{
    System.out.print(getEmail());
}

public void printCellNo()
{
    System.out.print(getCellNo());
}
public void printCategory()
{
    System.out.print(getCategory());
}



 }

You wrote the method you need!你写了你需要的方法!

public ArrayList<Contact> getContact() {
    return contacts;
}

Just do addressBookInstance.getContact() .只需执行addressBookInstance.getContact()

As a side note, I'd suggest renaming the method to getContacts or getContactList for clarity.作为旁注,为了清楚起见,我建议将该方法重命名为getContactsgetContactList You're returning a list of contacts, not a single contact.您正在返回联系人列表,而不是单个联系人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM