午夜剧场伦理_日本一道高清_国产又黄又硬_91黄色网战_女同久久另类69精品国产_妹妹的朋友在线

您的位置:首頁技術(shù)文章
文章詳情頁

Java地址簿。如何防止代碼中重復(fù)的聯(lián)系人?

瀏覽:180日期:2024-05-05 10:31:14
如何解決Java地址簿。如何防止代碼中重復(fù)的聯(lián)系人??

這是用于保留重復(fù)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);}

如果您想更改此代碼以保留重復(fù)的名稱,請執(zhí)行以下操作

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);}解決方法

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); elseSystem.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; }}

我正在嘗試創(chuàng)建一個通訊錄,其中每個聯(lián)系人都有一個名字,姓氏和唯一的ID。

我的問題是如何防止用戶輸入具有相同名字和姓氏的重復(fù)聯(lián)系人?我應(yīng)該在addContact方法中還是在main中實現(xiàn)某種檢查?怎么樣?

標(biāo)簽: java
相關(guān)文章:
主站蜘蛛池模板: 日韩成人一级 | 欧美精品久久久久久久久久 | 在线观看av资源 | 伊人久久婷婷 | 日本精品久久久久 | 极品久久久 | 国产伦精品一区二区三区视频黑人 | 四虎久久久| 日韩一区二区视频 | 超碰手机在线 | 超碰精品在线观看 | 四虎影院最新 | 精品免费久久久 | 麻豆国产免费 | 午夜看看 | 亚洲黄色免费网站 | 日韩小视频在线 | 日韩女优一区 | 欧美日韩一区二区三区四区五区 | 91亚洲国产成人久久精品麻豆 | 国产一区二区视频免费 | 麻豆av一区二区 | 免费成人毛片 | 成人永久免费 | 黄色777| 男女国产视频 | 国产小视频在线看 | 2019亚洲天堂| 亚洲在线中文字幕 | 欧美亚洲综合在线 | 2017天天干 | 欧美国产日韩一区二区三区 | 成人av免费观看 | 国产99自拍 | 国产精品欧美久久久久天天影视 | 毛片网站免费观看 | 欧美成人黄色片 | 日韩一区二区在线播放 | 精品国产一区二区三区久久久蜜臀 | 国产精品88av | 欲色av |