亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

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

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

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

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

如果您想更改此代碼以保留重復的名稱,請執(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。

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

標簽: java
相關(guān)文章:
主站蜘蛛池模板: 亚洲综合在线观看一区www | 国产各种盗摄视频 | cijilu刺激 国产免费的 | 伊人色婷婷综在合线亚洲 | 黄网免费 | 生活片一级播放免费 | 国产欧美日韩在线观看精品 | 永久免费看的啪啪网站 | 成人欧美在线 | 18hd xxxx国产在线| 亚洲精品欧洲一区二区三区 | 久久综合精品国产一区二区三区 | 欧美日韩综合精品一区二区三区 | 欧美一级一毛片 | 日本的黄色录像 | 91福利国产在线在线播放 | 一区二区三区四区亚洲 | 国产在线一二三区 | 国产精品免费观看视频播放 | 黄色片视频| 色综合久久98天天综合 | 成人日b视频| 爱爱欧美视频 | 日韩在线视精品在亚洲 | 免费精品美女久久久久久久久 | 免费中文字幕 | 一级做a爰片性色毛片新版的 | 99自拍视频在线观看 | 又亲又揉摸下面视频免费看 | 亚洲国产精品免费 | 亚洲日韩视频免费观看 | 国产呦系列 欧美呦 日韩呦 | 国产对白91色拍高清精品 | 巨乳毛片 | 色男天堂 | 黄色网页免费大全 | 一节毛片 | 欧美+日本+国产+在线观看 | 久久日本经典片免费看 | 日本精品久久久免费高清 | 欧美日韩一卡二卡 |