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

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

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

瀏覽:172日期: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
相關文章:
主站蜘蛛池模板: www.亚洲欧美 | 男女草逼视频 | 日本色www | 日本黄色高清 | 伊人久久精品 | 成人91看片 | 日韩成人精品一区二区 | 午夜毛片 | 青青艹在线观看 | 日批毛片 | 一直高潮(巨肉高h) 亚洲色图在线视频 | 在线观看中文字幕码 | 日本www在线 | 在线永久看片免费的视频 | 亚洲欧美日韩久久精品 | 国产日韩欧美综合在线 | 成年人午夜影院 | 欧美第一页在线 | 97超级碰| 日韩视频区 | 国产剧情自拍 | 日韩欧美视频在线免费观看 | 日韩在线欧美 | 美女特级毛片 | www.69av | 中文字幕一区二区三区视频 | www.久久艹 | 伊人久久精品 | 日本成人一区二区 | 色视频免费 | 亚洲欧洲一区二区 | 久久午夜视频 | 国产精品二三区 | 亚洲婷婷综合网 | 国产成人精品在线视频 | 韩国午夜影院 | 九九天堂| 国产免费av一区二区 | 热99视频| 在线aaa| 成人av影视 |