亚洲免费在线视频-亚洲啊v-久久免费精品视频-国产精品va-看片地址-成人在线视频网

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

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

瀏覽:94日期: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
相關文章:
主站蜘蛛池模板: 成年人免费大片 | 黄 色 成 年 人小说 | 国产精品久久久久久一级毛片 | 99re最新这里只有精品 | 又刺激又黄的一级毛片 | 国产一级毛片午夜 | 欧美成网| 成人永久免费视频网站在线观看 | 国产91香蕉在线精品 | 色综合久久88色综合天天 | 国内一级特黄女人精品片 | 美国一级特a黄 | 日韩在线观看中文字幕 | 免费视频一区二区三区四区 | 亚洲欧美日韩国产精品26u | 欧美一级色视频 | 欧美一级做 | 18videosex性欧美69超高清 | 精品一久久 | 国产日b视频 | 三级网站视频在线观看 | 国产精品美女一区二区 | 免费一级毛片正在播放 | 亚洲在线国产 | 欧美亚洲国产片在线观看 | 中文字幕视频网站 | 农村三级孕妇视频在线 | 欧美一级手机免费观看片 | 国产黄色免费网站 | 黄人成a动漫片免费网站 | 亚洲国产天堂久久综合图区 | 在线观看国产亚洲 | 久久香蕉国产精品一区二区三 | 一级一级毛片看看 | 欧美在线乱妇一级毛片 | 日韩三级一区二区 | 99热精品在线观看 | yy6080久久亚洲精品 | 欧美一级在线毛片免费观看 | 手机看片亚洲 | 精品视频在线视频 |