switch(menuChoice) {
case 1:
System.out.println("Enter your contact's first name:\n");
String fname = scnr.next();
System.out.println("Enter your contact's last name:\n");
String lname = scnr.next();
Necronomicon.addContact(new Person(fname, lname));
break;
// main truncated here for readability
import java.util.ArrayList;
public class AddressBook {
ArrayList<Person> ArrayOfContacts= new ArrayList<Person>();
public void addContact(Person p) {
ArrayOfContacts.add(p);
/*
for(int i = 0; i < ArrayOfContacts.size(); i++) {
if(ArrayOfContacts.get(i).getID() != p.getID())
ArrayOfContacts.add(p);
else
System.out.println("Sorry this contact already exists.");
}
*/
}
}
public class Person {
private String fName = null;
private String lName = null;
private static int ID = 1000;
public Person(String fName, String lName) { // Constructor I'm using to try and increment the ID each time a Person object is created starting at 1001.
this.fName = fName;
this.lName = lName;
ID = ID + 1;
}
}
我正在尝试创建一个地址簿,每个联系人都有一个名字,姓氏和唯一ID。
我的问题是如何阻止用户输入具有相同名字和姓氏的重复联系人?我应该在addContact方法中还是在main中实现某种检查?怎么样?
答案 0 :(得分:1)
您可以简单地使用HashSet并避免任何类型的循环来测试它。 HashSet负责此功能。
import java.util.Set;
import java.util.HashSet;
public class AddressBook {
Set<Person> listOfContacts = new HashSet<>();
public void addContact(Person p) {
if (!listOfContacts.add(p))
System.out.println("Sorry this contact already exists.");
}
}
要增加ID属性,您应该有2个属性,1个静态和另一个属性,并在构造函数中递增它。看:
public class Person {
private final int ID;
private static int id = 1000;
private String fName;
private String lName;
public Person(String fName, String lName) { // Constructor I'm using to try and increment the ID each time a Person object is created starting at 1001.
this.ID= ++id;
this.fName = fName;
this.lName = lName;
}
要使HashSet不接受重复的对象,您应该设置不应该在类中复制哪些属性(在您的情况下为Person)。举个例子:
@Override
public int hashCode() {
int hash = 7;
hash = 61 * hash + Objects.hashCode(this.fName);
hash = 61 * hash + Objects.hashCode(this.lName);
return hash;
}
@Override
public boolean equals(Object obj) {
if (obj == null || getClass() != obj.getClass())
return false;
final Person other = (Person) obj;
if (!Objects.equals(this.fName, other.fName))
return false;
return Objects.equals(this.lName, other.lName);
}
}
顺便说一句,您可以使用IDE(Eclipse,NetBeans等)生成equals
和hashCode
方法
修改
由于您无法使用HashSet,我将展示一个带有ArrayList的版本。顺便说一下,你必须使用HashCode
和equals
,因为我说要让它运作良好
import java.util.List;
import java.util.ArrayList;
public class AddressBook {
List<Person> arrayOfContacts = new ArrayList<>();
public void addContact(Person p) {
if (listOfContacts.contains(p))
System.out.println("Sorry this contact already exists.");
else
arrayOfContacts.add(p);
}
}
答案 1 :(得分:0)
使用HashSet。对于每个用户的名称,将其添加到HashSet。
例如:
HashSet<String> namesUsed = new HashSet<String>();
if (namesUsed.contains(userName)) {
//do what you want here, if this is entered it means there is a duplicate
} else {
namesUsed.add(userName); //add it to the list of used names
}
答案 2 :(得分:0)
以下是保留重复ID的代码。
public void addContact(Person p) {
for(int i = 0; i < ArrayOfContacts.size(); i++) {
Person contact = ArrayOfContacts.get(i);
if(contact.getID() == p.getID()) {
System.out.println("Sorry this contact already exists.");
return; // the id exists, so we exit the method.
}
}
// Otherwise... you've checked all the elements, and have not found a duplicate
ArrayOfContacts.add(p);
}
如果您想更改此代码以防止重复名称,请执行此类操作
public void addContact(Person p) {
String pName = p.getFname() + p.getLname();
for(int i = 0; i < ArrayOfContacts.size(); i++) {
Person contact = ArrayOfContacts.get(i);
String contactName = contact.getFname() + contact.getLname();
if(contactName.equals(pName)) {
System.out.println("Sorry this contact already exists.");
return; // the name exists, so we exit the method.
}
}
// Otherwise... you've checked all the elements, and have not found a duplicate
ArrayOfContacts.add(p);
}
答案 3 :(得分:0)
你应该在你的Person类中覆盖equals
public boolean equals(Object obj) {
if(fName.equals(obj.getfName()) && lName.equals(obj.getlName)) {
return true;
}
return false;
}
然后打电话:
if(!(person1.equals(person2))) {
//not a duplicate
}
当然,用您想要的任何对象替换变量/对象。 您还应该为姓氏和名字添加getter和setter。 希望这有帮助!