sha512boo
sha512boo

Reputation: 25

How to split a string in an array and make an associative array with it?

I have an array "arrayServers" with values. String[] arrayServers; and when I output data in a loop

for (int i = 0; i < arrayServers.length; i++){ Log.i(LOG_TAG,arrayServers[i]); }

I get these values:

91.134.166.76:8085 149.202.89.34:7776 176.32.36.18:7777 176.32.36.124:7777 195.201.70.37:7777 5.254.104.134:7777 176.32.37.82:7777

How do I display this list somehow like this:

[IP => 176.32.37.27 PORT => 7777, IP => 54.38.156.202 PORT => 7777, IP => 51.68.208.5 PORT => 7777]

the port has only 4 digits

Upvotes: 0

Views: 94

Answers (1)

Shashank Gupta
Shashank Gupta

Reputation: 347

class WebAddress{
    String ip;
    String port;
}

WebAddress[] webAddresses = new WebAddress[arrayServers.length];
WebAddress webAddress = new WebAddress();
for (int i = 0; i < arrayServers.length; i++){
      String strArr[] = arrayServers[i].split(":");
      webAddress.setIp(strArr[0]);
      webAddress.setPort(strArr[1]);
      webAddresses[i] = webAddress;
}

Then iterate over webAddresses array to get the values

Upvotes: 1

Related Questions