您好,登錄后才能下訂單哦!
本文實例為大家分享了java使用集合實現通訊錄的具體代碼,供大家參考,具體內容如下
代碼有些繁瑣,只適合初學者。
項目1
java通訊錄
方法(聲明方法的類)
代碼塊
代碼塊語法遵循標準markdown代碼,例如:
package com.lanou.homwork; import java.util.Collection; import java.util.HashMap; import java.util.LinkedList; import java.util.Scanner; import java.util.Set; class ContactPerson { public HashMap map = null; public HashMap getMap() { return map; } public void setMap(HashMap map) { this.map = map; } public ContactPerson(HashMap map) { super(); this.map = map; } public ContactPerson() { } // 添加聯系人 public HashMap addContact(Person person) { // map集合為空 if (this.map.size() == 0) { LinkedList list = new LinkedList(); // 添加到list集合里 list.add(person); this.map.put(person.getFirstCard(), list);// 添加map集合 System.out.println(person.getFirstCard()); } else { System.out.println("1111111111111111111"); // 不為空的時候.首先判斷有沒有這個分組 Set keySet = this.map.keySet();// 得到所有的鍵key // 遍歷所有鍵 for (Object object : keySet) { if (person.getFirstCard().equals(object)) { Object object2 = this.map.get(person.getFirstCard()); LinkedList linkedList = (LinkedList) object2; // 有這個分組 linkedList.add(person); } else { // 沒有這個分組 LinkedList list3 = new LinkedList(); list3.add(person); this.map.put(person.getFirstCard(), list3); System.out.println(keySet); System.out.println(map.values()); return map; } } } return map; } // 刪除聯系人 public void remove(Person person) { // 首先找到那個分組 // 所有的值 Set set = this.map.keySet(); for (Object object : set) { String nameString = person.getFirstCard(); if (nameString.equals(object)) { // 找見分組 Collection collection = this.map.values(); for (Object object1 : collection) {// list數組 LinkedList list1 = (LinkedList) object1;// 強制轉換 for (Object object2 : list1) {// 遍歷 Person person2 = (Person) object2;// if (person.getNameString().equals( person2.getNameString())) { if (list1.size() == 1) { this.map.remove(person2.getFirstCard());// 刪除整個分組 show(); } else { list1.remove(person2); show(); } } } } } } } // 展示方法 public void show() { System.out.println("*************************************"); System.out.println("請選擇功能"); String[] newArrayStrings = { "1.添加聯系人", "2.刪除聯系人", "3.修改聯系人", "4.展示所有聯系人" }; for (int i = 0; i < newArrayStrings.length; i++) { System.out.println("******" + newArrayStrings[i] + "******"); } System.out.println("*************************************"); Scanner scanner = new Scanner(System.in); int i = scanner.nextInt(); switch (i) { case 1: { addPerosn(); } break; case 2: { removePerson(); } break; case 3: { upPerson(); } break; case 4: { printResult(this.map); } break; default: break; } } // 修改聯系人信息 public void upPerson() { Person person = new Person(); System.out.println("請輸入要修改人的姓名:"); Scanner scanner = new Scanner(System.in); String naString = scanner.next(); person.setNameString(naString); shanChu(person); } // 輸入新的人名,年齡 public void cc(Person person) { Scanner scanner = new Scanner(System.in); System.out.println("請輸入新的名字:"); String naString2 = scanner.next(); System.out.println("請輸入新的年齡:"); int age = scanner.nextInt(); System.out.println("請輸入新的分組"); String group = scanner.next(); upPersonMessage(person, naString2, group, age); } // 判斷有沒有這個人 public void shanChu(Person person) { if (this.map.isEmpty()) { System.out.println("通訊錄為空,修改失敗"); System.out.println("是否添加聯系人?(Yes/No)"); Scanner scanner = new Scanner(System.in); String nameString = scanner.next(); if (nameString.equals("Yes")) { addPerosn(); } else { System.out.println("傻逼,滾你媽"); } } Collection collection = this.map.values(); for (Object object : collection) { LinkedList list = (LinkedList) object; for (Object object2 : list) { Person person2 = (Person) object2; if (person.getNameString().equals(person2.getNameString())) { System.out.println("恭喜你,通訊錄有此人"); cc(person); } else { System.out.println("輸入有誤"); } } } } public Person addPerosn() { Person p = new Person(); System.out.println("請輸入添加人名字:"); Scanner scanner = new Scanner(System.in); // 姓名 String name = scanner.next(); // 年齡 System.out.println("請輸入年齡:"); int age = scanner.nextInt(); // 分組 System.out.println("請輸入分組名稱:"); String firstCardString = scanner.next(); p.setFirstCard(firstCardString); p.setAge(age); p.setNameString(name); System.out.println(p); addContact(p); System.out.println("是否繼續操作?y/n"); if (scanner.next().equals("y")) { show(); } else { System.out.println("***************歡迎您下次再來****************"); } return p; } public Person removePerson() { Person p = new Person(); System.out.println("請輸入刪除人名字:"); Scanner scanner = new Scanner(System.in); // 姓名 String name = scanner.next(); // 年齡 System.out.println("請輸入刪除人的年齡:"); int age = scanner.nextInt(); // 分組 System.out.println("請輸入刪除的分組名稱:"); String firstCardString = scanner.next(); p.setFirstCard(firstCardString); p.setAge(age); p.setNameString(name); remove(p); return p; } // 修改聯系人 public HashMap upPersonMessage(Person person, String nameString, String firstCard, int age) { // 判斷有沒有此聯系人 if (this.map.isEmpty()) { System.out.println("通訊錄為空"); } else { String name = person.getFirstCard(); // 找聯系人 Collection collection = this.map.values();// 得到所有的值 for (Object object : collection) { LinkedList list = (LinkedList) object; for (Object object2 : list) { Person p = (Person) object2; if (person.getNameString().equals(p.getNameString())) { // 相等話,說明有這個人 System.out.println("有次聯系人"); if (list.size() == 1) { this.map.remove(p.getFirstCard()); Person person2 = new Person(); person2.setAge(age); person2.setFirstCard(firstCard); person2.setNameString(nameString); LinkedList list2 = new LinkedList(); list2.add(person2); this.map.put(person2.getFirstCard(), list2); System.out.println(this.map); } else { Person person2 = new Person(); person2.setAge(age); person2.setFirstCard(firstCard); person2.setNameString(nameString); list.add(person2); System.out.println(this.map); } } } } } return map; } // 顯示所有的聯系人 public void printResult(HashMap map1) { if (map1.isEmpty()) { System.out.println("通訊錄為空"); } else { System.out.println(map1); Scanner scanner = new Scanner(System.in); System.out.println("是否繼續y/n"); if (scanner.next().equals("y")) { show(); } else { System.out.println("***************歡迎您下次再來****************"); } } } } class Person { private String nameString; private String firstCard; private int age; public String getNameString() { return nameString; } public void setNameString(String nameString) { this.nameString = nameString; } public String getFirstCard() { return firstCard; } public void setFirstCard(String firstCard) { this.firstCard = firstCard; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public Person() { super(); // TODO Auto-generated constructor stub } public Person(String nameString, String firstCard) { this.nameString = nameString; this.firstCard = firstCard; } public Person(String nameString, String firstCard, int age) { this(nameString, firstCard); this.age = age; } @Override public String toString() { return "姓名:" + this.getNameString() + "年齡:" + this.age + "分組:" + this.firstCard; } }
腳注
項目2
測試類
代碼塊
代碼塊語法遵循標準markdown代碼,例如:
package com.lanou.homwork; import java.util.HashMap; public class TestPerson { public static void main(String[] args) { HashMap map = new HashMap(); ContactPerson contactperson = new ContactPerson(map); contactperson.show(); } }
感謝瀏覽。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。