工作分割小記錄
阿新 • • 發佈:2018-11-25
測試類
@Test
public void ipfeng() {
String[] clients = {"android:1.5.0,1.6.0,1.6.1", "apple:1.5.0,1.6.0,1.6.1", "m:1.5.0,1.6.0,1.6.1", "pc:1.5.0,1.6.0,1.6.1"};
ArrayList<String> list = new ArrayList<>();
getClientVersions(clients);
}
公共類
public static ArrayList<ClientVersion> getClientVersions(String[] clients) { //儲存物件 ArrayList<ClientVersion> result = new ArrayList<>(); for(String clientVersion : clients) { //分割物件 int sepIndex = clientVersion.indexOf(":"); System.out.println("sepIndex: "+sepIndex); //分割後獲前面欄位 String client = clientVersion.substring(0, sepIndex); System.out.println("client: "+client); //通過split繼續分割 String[] versions = clientVersion.substring(sepIndex + 1, clientVersion.length()).split(","); for(String version : versions) result.add(new ClientVersion(client, version)); } Iterator<ClientVersion> iterator = result.iterator(); while (iterator.hasNext()) { ClientVersion next = iterator.next(); System.out.println(next.getClient()+"...."+next.getClientVersion()); } return result; }
生存java實體
public class ClientVersion { public String client; public String clientVersion; public ClientVersion(String client, String clientVersion) { this.client = client; this.clientVersion = clientVersion; } public String getClient() { return client; } public String getClientVersion() { return clientVersion; } }