2014-03-03 92 views
0

在驱动程序中,它正确地找到位置,但我尝试使用contacts.remove(foundPosition) ...并且“联系人”未被删除。 如何删除用户搜索的整个“联系人”?如何删除ArrayList的对象?

司机:含有

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

public class contactDriver 
{ 
public static void main (String[] args) 
{      
      Scanner scan = new Scanner(System.in); 

      // Asks you to enter values from the given menu. 
      int answer; 
      System.out.println ("Press 1 to add a contact.");   // Menu is printed. 
      System.out.println ("Press 2 to display all contacts."); 
      System.out.println ("Press 3 to search for a contact."); 
      System.out.println ("Press 4 to search for a contact to delete."); 

      answer = scan.nextInt(); 

      // ArrayList 

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

     contacts.add(new Contact("Patrick", "McGee", "334-555-8860", "[email protected]")); 
     contacts.add(new Contact("John", "Appleseed", "142-555-6740", "[email protected]")); 
     contacts.add(new Contact("Matt", "Jordan", "213-555-2323", "[email protected]")); 
     contacts.add(new Contact("Kanye", "East", "255-434-9909", "[email protected]")); 
     contacts.add(new Contact("Derrick", "Flower", "144-555-1111", "[email protected]")); 


       // If statements for the user's decision. 

      if (answer == 4){ // User searches for a contact and chooses whether or not to delete the contact. 
       System.out.println("Enter the first name of the contact to search for."); 
       String fname = scan.next(); 
       scan.nextLine(); 

       System.out.println("Enter the last name of the contact to search for."); 
       String lname = scan.next(); 
       scan.nextLine(); 

       Collections.sort(contacts); 
       for(Contact c : contacts) 
       System.out.println(c); 

       int foundPosition = Collections.binarySearch(contacts, new Contact(fname, lname, "", "")); 
       System.out.println(foundPosition); 

       // The found contact may or may not be deleted. 

       String answer2; 
       System.out.println("Would you like to delete this contact? (y/n)"); 
       answer2 = scan.next(); 

       if (answer2 == "y"){ 
       contacts.remove(foundPosition); // **HERE is where I need help.** 
       System.out.println ("Contact is deleted. Here is the updated contact list: "); 

       for(Contact c : contacts) 
       System.out.println(c); 
       } 

       if (answer2 == "n"){ 
       System.out.println("Contact will not be deleted."); 
       } 

       } 
       } 
       } 

类方法:

import java.util.Collections; 
import java.util.ArrayList; 
import java.util.Comparator; 

public class Contact implements Comparable<Contact> 
{ 

public Contact(String fname, String lname, String num, String email) 
{ 
    this.fname = fname; 
    this.lname = lname; 
    this.num = num; 
    this.email = email; 
} 

public String getFirstname() 
{ 
    return fname; 
} 

public String getLastname() 
{ 
    return lname; 
} 

    public String getNum() 
    { 
    return num; 
    } 

    public String getEmail() 
    { 
    return email; 
    } 

    public int compareTo(Contact other) 
    { 
    if (fname.compareTo(other.lname) == 0) 
    return fname.compareTo(other.fname); 
    return lname.compareTo(other.lname); 
    } 

    public String toString() 
    { 
    return "Contact[" + fname + ", " + lname + ", " + num + ", " + email + "]"; 
    } 

    private String fname; 
    private String lname; 
    private String num; 
    private String email;  

}

比较:

import java.util.Comparator; 

class ContactComparator implements Comparator<Contact> 
{ 

public int compare(Contact a, Contact b) 
{ 

    if (a.getFirstname().compareTo(b.getFirstname()) == 0) 
    return a.getFirstname().compareTo(b.getFirstname()); 
    return a.getLastname().compareTo(b.getLastname()); 

} 

} 

感谢。

+4

我们需要更多的信息,我办看不到你要去的地方,你得到了什么输出... – libik

+2

显示你的完整代码。 –

+0

好的,有一刻 – pmcg521

回答

3

remove(int)不工作的原因是因为没有执行。

在你的情况是你错误地比较引用类型。

if (answer2 == "y"){ 
    contacts.remove(foundPosition); // **HERE is where I need help.** 

当使用对象来检查是平等的,你应该调用方法equals

if ("y".equalsIgnoreCase(answer2)){ // **HERE is where You needed help.** 
     contacts.remove(foundPosition); 

你可以阅读更多关于它在这里:

How do I compare strings in Java?

+0

非常感谢你! :) – pmcg521

相关问题