Java,按对象获取ArrayList索引 [英] Java, getting an ArrayList index by object

查看:835
本文介绍了Java,按对象获取ArrayList索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我在我的代码中遇到了一个小问题:

so I have hit a little problem in my code I have:

synchronized(clients)
            clients.remove(this);
}

当客户端断开连接时,但现在我需要能够发送该客户的名称给所有其他客户,为此我必须做类似的事情

for when a client disconnects, but now I need to be able to send the name of that client to all the other clients, and to do this I essentialy need to do something like

synchronized(clients)
            broadcast("Remove:"+clients.get(this).name);
            clients.remove(this);
}

但显然我无法获得带有this的索引,所以如何获得正确的客户名称?谢谢!

but obviously I can't get an index with the "this", so how do I go about getting the right clients name? Thanks!

推荐答案

为什么不简单地使用this.name?
因为你已经拥有了这个对象为什么你需要让索引再次获取对象?

why don't you simply use this.name? As you already have the object why do you need to get the index to again get the object?

编辑:

要回答标题中的问题(获取对象的索引),请使用 indexOf

To answer the question in the title(to get index of the object) use indexOf

这篇关于Java,按对象获取ArrayList索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆