0

Let's say you have an array like this:

protected ArrayList<String> client = new ArrayList<String>();

And then you do this:

client.add(ip, username);

What I am trying to do is, grab the username, using the IP. I only have the IP, I don't have the username, and therefore I need to use the IP to grab the username.

The IP is unique, can't have the same ip in the same array.

How can I use the IP to get the username?

1

1 Answer 1

6

You should use a Map with String key and value instead, and use the ip as key. A basic example:

Map<String, String> clients = new HashMap<String, String>();

//fill the map...
String ip = "127.0.0.1";
String name = "luiggi";
clients.put(ip, name);

//get the username by ip
System.out.println(clients.get(ip));
Sign up to request clarification or add additional context in comments.

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.