[英]How to remove the Object of an ArrayList?
在驱动程序中,它可以正确找到位置,但是我尝试使用contacts.remove(foundPosition)
...并且未删除“联系人”。 如何删除用户搜索的整个“联系人”?
司机:
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", "pmcgee123@gmail.com"));
contacts.add(new Contact("John", "Appleseed", "142-555-6740", "jappleseed99@gmail.com"));
contacts.add(new Contact("Matt", "Jordan", "213-555-2323", "mjordan23@gmail.com"));
contacts.add(new Contact("Kanye", "East", "255-434-9909", "keast123@gmail.com"));
contacts.add(new Contact("Derrick", "Flower", "144-555-1111", "dflower1@gmail.com"));
// 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());
}
}
谢谢。
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);
你可以在这里读更多关于它的内容:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.