av一区二区在线观看_亚洲男人的天堂网站_日韩亚洲视频_在线成人免费_欧美日韩精品免费观看视频_久草视

您的位置:首頁技術文章
文章詳情頁

Java地址簿。如何防止代碼中重復的聯系人?

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

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

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; }}

我正在嘗試創建一個通訊錄,其中每個聯系人都有一個名字,姓氏和唯一的ID。

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

標簽: java
相關文章:
主站蜘蛛池模板: 免费在线观看毛片 | 欧美成人久久 | 午夜高清 | 国产一区二区日韩 | 亚洲精品蜜桃 | 九九精品视频在线观看 | 在线播放a| 国内精品国产成人国产三级 | 亚洲欧美在线观看视频 | 亚洲视频中文字幕 | 双性呜呜宫交受不住了h | 成人中文字幕在线观看 | 国产精品视屏 | 男女插插插视频 | 欧美精品福利 | 中国一级毛片免费看 | 亚洲第一免费视频 | 伊人黄色| 亚洲h网站 | 亚洲精品免费看 | 天天看片天天爽 | 久久久久一区二区三区 | 99视频在线精品免费观看2 | 涩涩97| 黄色在线观看免费 | 日韩免费小视频 | 天天舔天天操 | 色噜噜狠狠一区二区三区果冻 | 91精品国产99久久久久久红楼 | 国产精品久久久久久久午夜 | 福利看片 | 日韩一区二区三免费高清在线观看 | 亚洲天堂网在线观看 | 亚洲精品99 | 国语对白做受69 | 五月婷婷在线播放 | 国产wwwwww | 国产视频999 | 国产高潮在线观看 | 欧美视频一区二区 | 中文字幕免费看 |