Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java, getting an ArrayList index by object

Tags:

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);
}

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

like image 243
Jeremy Sayers Avatar asked Dec 01 '22 05:12

Jeremy Sayers


2 Answers

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?

Edit:

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

like image 159
Ankur Avatar answered Dec 02 '22 19:12

Ankur


Did you look at the indexOf function in ArrayList?

like image 44
randominstanceOfLivingThing Avatar answered Dec 02 '22 17:12

randominstanceOfLivingThing