国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

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

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

瀏覽:90日期: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
相關文章:
主站蜘蛛池模板: 91av成年影院在线播放 | 亚色网址| 国内精品久久久久久久影视麻豆 | 美国毛片毛片全部免费 | 国产精品亚洲精品日韩已方 | 欧美视频精品一区二区三区 | 亚洲国产欧美精品一区二区三区 | 日本在线www | 毛片激情永久免费 | 国产成人啪精品午夜在线观看 | 日韩三级黄色 | 亚洲天堂一区二区在线观看 | 在线观看视频中文字幕 | 久久久久成人精品一区二区 | 在线中文 | 99精品久久久久久久免费看蜜月 | 精品国产一区二区三区在线 | 玖玖爱精品| 国产三香港三韩国三级不卡 | 美女被cao免费看在线看网站 | 日韩欧美在线精品 | 综合在线亚洲 | 一级一级毛片免费播放 | 亚洲成人三级 | 久久99亚洲精品久久久久网站 | 精品久久久久久久久免费影院 | 国产精品日本 | 久久99久久精品国产99热 | 最近中文字幕免费视频 | 国产欧美精品区一区二区三区 | 欧美高清亚洲欧美一区h | 国产大陆亚洲精品国产 | 国产精品免费视频一区 | 免费看一级做a爰片久久 | 中国老太性色xxxxxhd | 亚洲美女免费视频 | 日韩亚洲人成网站在线播放 | 成人全黄三级视频在线观看 | 亚洲成年网站在线观看 | 91.xxx.高清在线| 久青草国产在线 |