我不确定我是否正确地表达了自己的标题,但我会尝试在这里更具体。
我有什么:具有客户端/客户的数组列表。一个数组列表,其中包含分配给客户/客户的电话号码。
我的客户端类: 导入 java.util.*;
public class Clients implements Comparable<Clients> {
private String name;
private String address;
public ArrayList<Share> shareList = new ArrayList<Share>();
private PhoneBook phoneBook = new PhoneBook();
public Clients(String name, String address) {
this.name = name;
this.address = address;
}
public void addPhoneDescription(String description) {
phoneBook.addPhoneDescription(description);
}
public void addPhoneNumber(String number) {
phoneBook.addPhoneNumber(number);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public PhoneBook getPhoneBook() {
return phoneBook;
}
public boolean equals(Object obj) {
Clients c = (Clients) obj;
return this.name.equals(c.name);
}
public String toString() {
String result;
result = ("n" + "Name: " + this.getName() + "n" + "Address: "
+ this.getAddress() + "n" + "Phone description: " + this
.getPhoneBook());
return result;
}
public int compareTo(Clients other) {
return name.compareTo(other.name);
}
}
这是我的电话簿类,由set/get方法组成:
import java.util.ArrayList;
public class PhoneBook {
private ArrayList<String> numbersList = new ArrayList<String>();
private ArrayList<String> phoneDescription = new ArrayList<String>();
public void addPhoneDescription(String description) {
phoneDescription.add(description);
}
public void addPhoneNumber(String number) { // Add the phone number to the numbersList
numbersList.add(number);
}
public String toString(){
return numbersList.toString() + phoneDescription.toString();
}
}
我想要实现的目标:因此,如果我创建假设 3 个客户端并且其中 2 个具有相同的数字,我想打印出这 2 个客户端以及它们共享/具有的共同编号,依此类推。我在程序类上创建了一个方法:
public void findDuplicatedNumbers() {
// method that looks for duplicated numbers that clients have
ArrayList<Integer> sNumber = new ArrayList<>();
ArrayList<Integer> duplicateNumber = new ArrayList<>();
for (int i = 0; i < clientList.size(); i++) {
for (int k = 0; k < (clientList.get(i).getPhoneBook().size); k++) {
if (sNumber.contains(clientList.get(i).getPhoneBook().get(k).getNumber())) {
if (duplicateNumber.contains(clientList.get(i).getPhoneBook().get(k).getNumber())) {
} else {
// adds to duplicateNumber arrayList
duplicateNumber.add(clientList.get(i).getPhoneBook().get(k).getNumber());
}
} else {
// adds to sNumber arrayList
sNumber.add(clientList.get(i).getPhoneBook().get(k).getNumber());
}
}
}
for (int i = 0; i < duplicateNumber.size(); i++) {
System.out.println("Phone number: " + duplicateNumber.get(i) + " is share with these clients: ");
for (int k = 0; k < clientList.size(); k++) {
for (int p = 0; p < (clientList.get(p).getPhoneBook().size()); p++) {
if (duplicateNumber.get(i) == clientList.get(k).getPhoneBook().get(p).getNumber()) {
System.out.println(clientList.get(k).getName() + ", ");
}
}
}
System.out.println("n");
}
}
Puh .. 为一个简单的任务安静了很多代码。亲吻(保持简单):
public void findDuplicatedNumbers() {
Map<String, Set<Client> > duplicates =
new HashMap<String, Set<Client> >();
Set<String> phoneNumbers = new HashSet<String>();
for(Client client : clientList) {
PhoneBook phoneBook = client.getPhoneBook();
phoneNumbers.addAll(phoneBook.getNumberList());
}
for(String phoneNumber : phoneNumbers) {
Set<Client> clients = findClientsByPhoneNumber(phoneNumber);
if(clients.size() > 1)
duplicates.put(phoneNumber, clients);
}
for(Entry<String, Set<Client> entry : duplicates.entrySet()) {
System.out.println("phonenumber " + entry.getKey() + " is dubplicated / is share with these clients:");
for(Client client : entry.getValue()) {
System.out.println(client.getName());
}
}
}
protected Set<Client> findClientsByPhoneNumber(String phoneNumber) {
Set<Client> clients = new HashSet<Client>();
for(Client client : clientList ) {
List<String> phoneNumbers = client.getPhonebook().getNumberList();
if(phoneNumbers.contains(phoneNumber)) {
clients.add(client);
}
}
return clients;
}
这不仅结构更简洁,并实现了用于按编号查找客户端的附加接口,而且在大多数情况下具有更高的性能。只要明确你想要做什么:如果你想存储一个唯一对象的列表,它是Set - 实现之一。如果要通过键存储某些内容,请使用 Map - 实现。
public class PhoneBook {
private ArrayList<String> numbersList = new ArrayList<String>();
private ArrayList<String> phoneDescription = new ArrayList<String>();
public void addPhoneDescription(String description) {
phoneDescription.add(description);
}
public void addPhoneNumber(String number) { // Add the phone number to the numbersList
numbersList.add(number);
}
public String toString(){
return numbersList.toString() + phoneDescription.toString();
}
public ArrayList<String> findDuplicatedNumbers() {
ArrayList<String> dupes = new ArrayList<String>();
Collections.sort(numbersList);
Iterator<String> iter = numbersList.iterator();
while(iter.hasNext()) {
String next = iter.next();
if(iter.hasNext() && next == iter.next()) {
dupes.add(next);
}
}
return dupes;
}
}