2013-08-28 144 views
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; 
} 
} 
+2

您一直在创建一个新的数组列表,而不是使用单个列表。 –

+0

但是我不需要创建一个新的阵列,因为我添加了新的联系人? –

+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,而不是所有的信息。

编辑:

你 “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'."

可以明显改变,如果找到该名字,我只是简化了它会发生什么。

+0

非常感谢。我不敢相信我忽略了这一点。这一定会解决它。 –

+0

嗯。那很有意思。我仍然得到相同的结果。 –

+0

你的代码中有几个错误,它们中的任何一个都可能导致问题。这只是您提问的解决方案。 – Aaron

0

每次你想在你的主要方法插入你从ArrayList中

information = new ArrayList<String>(); 

创建新对象initizalize此ArrayList的名称然后通过它的变量访问它(信息)

0

直接的问题是与fi在inputData()方法中第一行:

information = new ArrayList<String>(); 

你正在创建的每个方法被调用时,新的ArrayList对象,这意味着旧的对象,它包含的数据,都将丢失。