我不确定我是否正确地表达了自己的标题,但我会在这里尝试更具体。
我有什么: 客户/客户 arraylist 。 将电话号码分配给客户/客户的 arraylist 。
我的客户类: import 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);
}
}
这是我的 PhoneBook类,包含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个具有相同的数字,我想打印出这两个客户端共享/共享的数量,依此类推。我在程序类中创建了一个方法:
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");
}
}
答案 0 :(得分:0)
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 - implementation之一。如果您想通过密钥存储某些内容,请使用Map - implementation。
答案 1 :(得分:0)
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;
}
}