在地址簿中存储信息

时间:2013-08-28 22:07:42

标签: java arraylist

我需要在不使用数据库(内存)的情况下制作地址簿应用程序。我决定使用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;
}
}

3 个答案:

答案 0 :(得分: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'."

如果找到名字,你可以明显地改变发生的事情,我只是简化了它。

答案 1 :(得分:0)

每次要插入名称时,都要从ArrayList

创建新的Object
 information = new ArrayList<String>();

在main方法中启动此arraylist,然后通过其变量(信息)访问它

答案 2 :(得分:0)

当前问题出在inputData()方法的第一行:

information = new ArrayList<String>();

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