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

查看:33
本文介绍了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天全站免登陆