Java 拆分字符串并存储在数组列表中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24256199/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
split string and store in arraylist
提问by user3691107
I want to split the below mentioned string and save it in two seperate arraylist like state and city
我想拆分下面提到的字符串并将其保存在两个单独的数组列表中,例如州和城市
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);
}
}
}
}
}
}
}
}
I have splitted each and every character, but i have to store state in seperate list and city in seperate list
我已经拆分了每个字符,但我必须将状态存储在单独的列表中,并将城市存储在单独的列表中
回答by prasadmadanayake
Here is the example
这是例子
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());
}
}
回答by mahesh
Step 1:Split your given string input as Arrays.asList(city.split("::"));
, you alresy did it.
第 1 步:将给定的字符串输入拆分为Arrays.asList(city.split("::"));
,您已经做到了。
Step 2:Split each list in to array like, example Tamilnadu;chennai-madurai-salem
using String both[]=string.split(";");
here you will get seperated state and cities. like both[0] is State
. both[1] is chennai-madurai-salem
第 2 步:将每个列表拆分为数组,例如,Tamilnadu;chennai-madurai-salem
使用 String both[]=string.split(";");
此处的示例您将获得单独的州和城市。喜欢both[0] is State
。两者 [1] 是chennai-madurai-salem
Step 3:Split the cities string in both[1] usig both[1].split("-")
第 3 步:在 both[1] usig 中拆分城市字符串both[1].split("-")
So Demo code I have given as below. You can modify.
所以我给出的演示代码如下。你可以修改。
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+" ");
}
}