在 Java 中进行等价

Making an equivalence in Java

正如您可能从我的代码中看到的那样,我对此很陌生。我正在尝试编写一个简单的程序来检索一个人的朋友。这是我的代码:

public class Person {
    private String name;
    private String friends;

    public Person(String aName) {
        name = aName;
        friends = "";
    }

    public String getFriends() {
        return friends;
    }

    public void addFriend(Person friend) {
        friends = friends + " " + friend.name; 
    }

    public void unfriend(Person nonFriend) {
        friends = friends.replace(" " + nonFriend.name, "");
    }

    public static void main(String[] args) {
        Person dana = new Person("Dana");
        Person gina = new Person("Gina");
        Person john = new Person("John");

        dana.addFriend(gina);
        dana.addFriend(john);
        john.addFriend(gina);
        john.addFriend(dana);
        john.unfriend(dana);

        System.out.println("Dana's friends are: " + dana.getFriends());
        System.out.println("Gina's friends are: " + gina.getFriends());
        System.out.println("John's friends are: " + john.getFriends());
    }
}

一切正常,但我不知道如何创建一个会说: 如果 Gina 既是 Dana 的朋友又是 John 的朋友,那么很明显 Gina 的朋友将是 Dana 和 John。我知道我可以在 gina.addFriend(dana)gina.addFriend(john) 处添加两行来实现相同的结果,但我想知道这样做的方法是什么。提前致谢。

我建议使用 persons/friends 的 arrayList 而不是一个字符串。

ArrayList<Person> friends = new ArrayList<Person>; 

您可以通过键入以下内容添加朋友:

public void addFriend(Person newFriend){ 
    friends.add(newFriend);
    newFriend.friends.add(this);
}

您通过键入以下内容删除好友:

public void unfriend(Person noFriend){
    for(int i = 0; i < this.friends.size(); i++){
        if(this.friends.get(i).name.equals(noFriend.name){
            this.friends.delete(i);
        }
    }
    for(int i = 0; i < noFriend.friends.size(); i++){
        if(noFriend.friends.get(i).name.equals(this.name){
            noFriend.friends.delete(i);
        }
    }
}

显示整个好友列表:

public void showFriends(){
    for(int i = 0; i < this.friends.size(); i++){
        System.out.println(this.friends.get(i));
    }
}

首先,将friends设为ArrayList<Friend>

private ArrayList<Friend> friends;

这有几个优点,包括更易于使用和存储 Person 本身,而不仅仅是它们的名称。

然后,将addFriend方法改为双向:

public void addFriend(Person friend) {
    friends.add(friend);
    friend.friends.add(this);
}

这样,友情就会自动往复。您还应该更改 unfriend:

public void unFriend(Person friend) {
    friends.remove(friend);
    friend.friends.remove(this);
}

编辑:根据上面的评论,一组实际上会更好,因为每个值只能有一个。

我会使用一个 Set,并为 person 添加一个唯一的 Id,以解决多人同名的问题。

您的 class 将如下所示:

public class Person
{
    private final String personId;
    private final String name;
    private final Set<Person> friends;

     public Person(String personId, String name) {
        super();
        this.personId = personId;
        this.name = name;
        this.friends = new HashSet<Person>();
    }

     public void addFriend(Person friend) {
         if(friend != null && !friends.contains(friend)) {
             this.friends.add(friend);
             // Optional : if it is a two-way relationship that doesn't need approving etc
             friend.addFriend(this);
         }
     }

     public void unfriend(Person nonFriend)
     {
         if(nonFriend != null && friends.contains(nonFriend)) {
             this.friends.remove(nonFriend);
             // Optional : if it is a two-way relationship that doesn't need approving etc
             nonFriend.unfriend(this);
         }
     }

     public Set<Person> getFriends()
     {
         return friends;
     }

    @Override
    public String toString() {
        return "Person [name=" + name + "]";
    }

    public static void main(String[] args)
    {
        Person dana = new Person("D001", "Dana");
        Person gina = new Person("G001", "Gina");
        Person john = new Person("J001", "John");


        dana.addFriend(gina);
        dana.addFriend(john);
        john.addFriend(gina);
        john.addFriend(dana);
        john.unfriend(dana);



        System.out.println("Dana's friends are: "+dana.getFriends());
        System.out.println("Gina's friends are: "+gina.getFriends());
        System.out.println("John's friends are: "+john.getFriends());

    }


    // Equals and Hashcode are very important when using 'contains' and other Set-based methods

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((personId == null) ? 0 : personId.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Person other = (Person) obj;
        if (personId == null) {
            if (other.personId != null)
                return false;
        } else if (!personId.equals(other.personId))
            return false;
        return true;
    }
}