4

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!

1
  • 1
    i think you want to remove object from list. why do u need to get index.simply u cam use list.remove(object); or list.remove(inndex); Commented Sep 1, 2012 at 5:08

4 Answers 4

13

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

Sign up to request clarification or add additional context in comments.

1 Comment

You sir, are correct, I have to stop doing late night programming hahahah!
5

Did you look at the indexOf function in ArrayList?

Comments

3
int index = clients.indexOf(this);
// Do what ever...
clients.remove(index); // or clients.remove(this);

Comments

-1

I think you want to remove specific object from list. If you get index from your code

int index = clients.get(this)

Then you can remove easily

clients.remove(index);

or if you get object from list then remove

clients.remove(object) // remove by object

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.