0

我需要在不使用数据库(在内存上)的情况下制作地址簿应用程序。我决定使用 ArrayLists 来做到这一点。但问题是,一旦我输入了新的姓名/联系人,它就会覆盖我之前“存储”(或认为我存储)的任何其他联系人。我一直在试图弄清楚并且完全困惑。

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

public class Main {

public static void main(String[] args) {
    firstActions();
}

static String firstName;
static String lastName;
static String phoneNumber;
static String search = null; 
static public int choice = 0;
static Scanner input = new Scanner (System.in);
static ContactInformation contact;
static ArrayList<String> information = new ArrayList<String>();

public static void firstActions()

{
    System.out.println("Address Book Menu: What would you like to do? 1) Input data. 2) Search data. 3) Close.");
    choice = input.nextInt();
    switch (choice) {
    case 1:
        inputData();
    case 2:
            System.out.println("Search by: 1) First Name 2) Last Name 3) Phone Number 4) Zip Code.");
            choice = input.nextInt();
            switch (choice) {
            case 1:
                searchName();
                break;
            case 2:
                searchLastName();
            case 3:
                searchPhoneNumber();
            case 4:
                //execute search by Zip Code
            default:
                System.out.println("Please compile again.");
                break;
            }
            break;
    case 3:
            System.out.println("Application terminated.");
            System.exit(0);
    default:
        System.out.println("Please compile again.");
        break;
    }

}
public static void inputData ()
{
    information = new ArrayList<String>();
    contact = new ContactInformation(firstName, lastName, phoneNumber, information);
    System.out.println("What is your first name?");
    contact.setFirstName(input.next());
    information.add(contact.getFirstName());
    System.out.println("What is your last name?");
    contact.setLastName(input.next());
    information.add(contact.getLastName());
    System.out.println("What is your phone number?");
    contact.setPhoneNumber(input.next());
    information.add(contact.getPhoneNumber());
    System.out.println("Saved.");
    System.out.println("What would you like to do next?");
    firstActions();
}
public static void searchName()
{
    System.out.println("What is the first name you are looking for?");
    search = input.next();
    if (search.equals(information.get(0)))
            {
                System.out.println(information);
                System.out.println("What would you like to do next?");
                firstActions();
            }
    else
    {
        System.out.println("This person is not saved in the address book. Please try again.");
        firstActions();
    }
}
public static void searchLastName()
    {
        System.out.println("What is the last name you are looking for?");
        search = input.next();
        if (search.equals(information.get(1)))
                {
                    System.out.println(information);
                    firstActions();
                }
        else
        {
            System.out.println("This person is not saved in the address book. Please try again.");
            firstActions();
        }
}
public static void searchPhoneNumber()
{
    System.out.println("What is the last name you are looking for?");
    search = input.next();
    if (search.equals(information.get(2)))
            {
                System.out.println(information);
                firstActions();
            }
    else
    {
        System.out.println("This person is not saved in the address book. Please try again.");
        firstActions();
    }
}
}

这是我的联系信息类:

import java.util.ArrayList;


public class ContactInformation {

public String firstName;
public String lastName;
public String phoneNumber;
ArrayList <String> information = new ArrayList<String> ();

public ContactInformation(String firstName, String lastName,
        String phoneNumber, ArrayList<String> information) {
    super();
    this.firstName = firstName;
    this.lastName = lastName;
    this.phoneNumber = phoneNumber;
    this.information = information;
}
public String getFirstName() {
    return firstName;
}
public void setFirstName(String firstName) {
    this.firstName = firstName;
}
public String getLastName() {
    return lastName;
}
public void setLastName(String lastName) {
    this.lastName = lastName;
}
public String getPhoneNumber() {
    return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
    this.phoneNumber = phoneNumber;
}
}
4

3 回答 3

1

您首先在此处创建 ArrayList:

static ArrayList<String> information = new ArrayList<String>();

但是每次你去这个inputData()方法时,你都会创建一个新的 ArrayList:

information = new ArrayList<String>();

从您编写代码的方式来看,我假设您有一个ContactInformation应该放入 ArrayList 的对象。

将 ArrayList 更改为:static ArrayList<ContactInformation> information = new ArrayList<ContactInformation>();

然后您可以创建每个对象并将该对象分别添加到所有信息的 ArrayList INSTEAD 中。

编辑:

您的“ContactInformation”对象包含String变量。将此对象添加到 ArrayList 后,您可以使用循环来查找对象中的数据是否与您要查找的数据匹配。它应该看起来像这样:

for (int i = 0; i != information.size(); i++) {
    if (information.get(i).getFirstName().matches(search)) {                
        System.out.println("found");
    }
}

if statement说_"if the element 'i's variable 'firstName' in ArrayList 'information' matches the variable 'search', print the word 'found'."

如果找到名称,您显然可以更改发生的情况,我只是简化了它。

于 2013-08-28T22:14:19.187 回答
0

每次要插入名称时,您都是从 ArrayList 创建一个新对象

 information = new ArrayList<String>();

在您的主要方法中初始化此数组列表,然后通过其变量(信息)访问它

于 2013-08-28T22:14:14.567 回答
0

最直接的问题是您的 inputData() 方法的第一行:

information = new ArrayList<String>();

每次调用该方法时,您都会创建一个新的 ArrayList 对象,这意味着旧对象及其包含的数据都将丢失。

于 2013-08-28T22:14:15.477 回答