2014-06-17 59 views
4

我想分割下面提到的字符串,並將其保存在像國家和城市的兩個單獨的ArrayList分割字符串,並存儲在ArrayList中

public class RoundValue { 

    public static void main(String args[]) { 
     String firstset = null; 

     String city = "Tamilnadu;chennai-madurai-salem::Kerala;cochin-tiruvandrum-calicut"; 

     ArrayList<String> mState = new ArrayList<String>(); 
     ArrayList<String> mCity = new ArrayList<String>(); 

     HashMap<String, List<String>> hashsplit = new HashMap<String, List<String>>(); 

     List<String> splitword1 = Arrays.asList(city.split("::")); 

     if (splitword1.size() > 0) { 
      for (int i = 0; i < splitword1.size(); i++) { 
       firstset = splitword1.get(i); 


       List<String> firststate = Arrays.asList(firstset.split("-")); 
       if (firststate.size() > 0) { 
        for (int j = 0; j < firststate.size(); j++) { 
         String firstcity = firststate.get(j); 

         List<String> secondcity = Arrays.asList(firstcity.split(";")); 
         if (secondcity.size() > 0) { 
          for (int k = 0; k < secondcity.size(); k++) { 
           String septcity = secondcity.get(k); 
           System.out.println("septcity Splitted:" + septcity); 

          } 
         } 
        } 
       } 
      } 
     } 

    } 
} 

我已經分裂的每一個字,但我必須存儲在狀態在單獨的列表

+0

什麼值應該在兩個列表中結束? – succcubbus

+0

在一個列表狀態(Tamilnadu,喀拉拉邦)和另一個列表城市(欽奈,馬杜賴,....) – user3691107

回答

1

這裏單獨列表,城市是例如

import java.util.ArrayList; 
import java.util.Arrays; 

/** 
* Created by Prasad on 6/17/2014. 
*/ 
public class stack { 
    public static void main(String[] args) { 
     String s="Tamilnadu;chennai-madurai-salem::Kerala;cochin-tiruvandrum-calicut"; 
     ArrayList<String> x1=new ArrayList<String>(); 
     ArrayList<String> x2=new ArrayList<String>(); 
     String string[]=s.split("::"); 
     for(String y:string){ 
      try{ 
       String[] temp=y.split(";"); 
       x1.add(temp[0]); 
       x2.add(temp[1]); 
      }catch(Exception e){} 
     } 
     System.out.println(x1.toString()); 
     System.out.println(x2.toString()); 
    } 

} 
3

第1步:分裂您給我的字符串輸入爲Arrays.asList(city.split("::"));,你改爲做了。

第2步:拆分每個列表中數組一樣,例如Tamilnadu;chennai-madurai-salem使用String both[]=string.split(";");在這裏,你會得到分隔的狀態和城市。像both[0] is State。既[1]是chennai-madurai-salem

步驟3:拆分城市串在兩個[1] USIG both[1].split("-")

我給如下所以演示代碼。你可以修改。

public static void main(String[] args) { 
     String city = "Tamilnadu;chennai-madurai-salem::Kerala;cochin-tiruvandrum-calicut"; 
     ArrayList<String> mState = new ArrayList<String>(); 
     ArrayList<String> mCity = new ArrayList<String>(); 
     List<String> bothList= Arrays.asList(city.split("::")); 

     for (String string : bothList) { 
      String both[]=string.split(";"); 
      String state=both[0]; 
      List<String> tempCityList=Arrays.asList(both[1].split("-")); 
      mState.add(state); 
      mCity.addAll(tempCityList); 
     } 
     System.out.println("Your states"); 
     for (String string : mState) { 
      System.out.print(string+" "); 
     } 
     System.out.println("\nYour cities"); 
     for (String string : mCity) { 
      System.out.print(string+" "); 
     } 


    }