当前位置: 首页 > 面试题库 >

Java地址簿。如何防止代码中重复的联系人?

西门靖琪
2023-03-14
问题内容
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中实现某种检查?怎么样?


问题答案:

这是用于保留重复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);

}


 类似资料:
  • 问题内容: 我有一个下面的HashMap(假设它有10,0000个元素) ========================== 我需要Java代码来防止在HashMap 中出现以下情况。 1> 2> 希望它清除。我们将不胜感激提供的Java代码。(由于我可以将任何重复项添加到现有地图中,因此需要通用解决方案) 问题答案: 你可以用一个类,它代表,以及其他的方法你使用。这种方法是一种浪费,但安全的,

  • 问题内容: 我试图了解如何创建查询以基于内部联接过滤掉一些结果。 考虑以下数据: 我想选择project_id为1的所有composition_batch记录,并具有component_id为1或2的composition_batch_component。因此,我运行以下查询: 但是,这将返回重复的条目: 有没有一种方法可以修改此查询,以便仅获取与条件匹配的唯一composition_batch记

  • 1、通过JavaScript屏蔽提交按钮(不推荐) 2、给数据库增加唯一键约束(简单粗暴) 3、利用Session防止表单重复提交(推荐) 4、使用AOP自定义切入实现

  • 可能重复: 如何检查在Java中相乘两个数字是否会导致溢出? 假设我有一个Java类方法,它使用和操作。 如何确保中没有发生溢出? 我想我可以使用BigDecimal,也可以用“包装器”替换all and*,比如: 有没有更好的方法来确保Java方法中不会发生int溢出?

  • 问题内容: 我有点困惑,PHP中有很多函数,有些使用这个,有些使用那个。有些人使用:,,等 哪个是正确的,你们通常使用什么? 这是正确的吗(如果有的话,建议我做一个更好的): 该行可以防止MySQL注入和XSS攻击? 顺便说一句,除了XSS攻击和MySQL注入之外,还有其他需要注意的事情吗? 编辑 结论: 如果我想将字符串插入数据库,则无需使用,只需使用即可。在显示数据时,请使用,这是您的全部意思