如何在数组中拆分字符串并与之建立关联数组?

时间:2019-06-15 10:45:21

标签: java string split substring

我有一个带有值的数组“ arrayServers”。 String[] arrayServers; 当我循环输出数据时

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

我得到这些值:

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

如何以这种方式显示此列表:

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

该端口只有4位数字

1 个答案:

答案 0 :(得分:1)

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

然后遍历webAddresses数组以获取值