[英]JAVA - about ArrayList
Hi i have a question about ArrayList in Java. 嗨,我对Java中的ArrayList有疑问。
I have in a class called "Utente" an ArrayList ricFriends; 我在一个名为“ Utente”的类中有一个ArrayList ricFriends。 Now, the problem is that i have a method called
现在,问题是我有一个方法叫做
requestFriendship(Utente u)
where i want to add in the u.ricFriends the object Utente that call the method... 我想在u.ricFriends中添加调用方法的对象Utente ...
I tried in this way but it's at 99% wrong.. 我以这种方式尝试过,但有99%的错误。
public void requestFriendship(Utente u){
u.ricFriends.add(this);
}
for example i want to do this : 例如我想这样做:
Utente Gary = new Utente();
Utente Mike = new Utente();
Gary.requestFriendship(Mike);
And if i check Mike.ricFriends i can see the object Gary; 如果我查看Mike.ricFriends,我可以看到对象Gary。
Sorry for the english, thank you. 对不起英语,谢谢。
Not sure what you're trying to do, but you should change this line: 不确定您要做什么,但是您应该更改此行:
u.ricFriends.add(this);
for this: 为了这:
this.ricFriends.add(u)
First of all, you are making a attribute public, that's not a good practice in OO programming, you should create get and set methods. 首先,您要公开一个属性,这不是OO编程中的好习惯,您应该创建get和set方法。
About you problem, you could do it something like this: 关于您的问题,您可以执行以下操作:
ArrayList<Utente> utenteList = u.getRicFriends();
utenteList.add(Mike);
Take care about "this" pointer, "this" mean that you are taking the object reference himself. 注意“ this”指针,“ this”表示您自己正在使用对象引用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.