gpt4 book ai didi

JAVA——关于ArrayList

转载 作者:行者123 更新时间:2023-12-02 03:43:40 27 4
gpt4 key购买 nike

您好,我有一个关于 Java 中 ArrayList 的问题。

我在一个名为“Utente”的类中有一个 ArrayList ricFriends;现在,问题是我有一个名为

的方法
requestFriendship(Utente u) 

我想在 u.ricFriends 中添加调用该方法的对象 Utente...

我尝试过这种方式,但99%都是错误的..

public void requestFriendship(Utente u){
u.ricFriends.add(this);
}

例如我想这样做:

Utente Gary = new Utente();
Utente Mike = new Utente();

Gary.requestFriendship(Mike);

如果我检查 Mike.ricFriends,我可以看到对象 Gary;

对不起,英语不好,谢谢。

最佳答案

不确定您要做什么,但您应该更改此行:

 u.ricFriends.add(this);

为此:

  this.ricFriends.add(u)

关于JAVA——关于ArrayList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36553764/

27 4 0