我的arraylist由一个包含字符串列表的数组组成,每个字符串内部用逗号分隔,我想将其中一个字符串拆分为以逗号分隔的子字符串,我知道如何通过使用拆分将其分配到数组方法,但我很难找到类似于数组列表的东西,继承代码:
String[] widgets2 =
{
"1,John,Smith,John1989@gmail.com,20,88,79,59",
"2,Suzan,Erickson,Erickson_1990@gmailcom,19,91,72,85",
"3,Jack,Napoli,The_lawyer99yahoo.com,19,85,84,87",
"4,Erin,Black,Erin.black@comcast.net,22,91,98,82",
"5,Adan,Ramirez,networkturtle66@gmail.com,100,100,100"
};
ArrayList<String> jim = new ArrayList<String>(Arrays.asList(widgets2));
System.out.println(jim.split(0));
答案 0 :(得分:1)
它类似于您对arrays
所做的操作。只是实施是不同的。要让值循环遍历它们:
List<String> jim = new ArrayList<String>(Arrays.asList(widgets2));
for(String currentString : jim){//ArrayList looping
String[] separatedStrings = currentString.split(",");
for(String separatedString : separatedStrings){//Now its array looping
//Do something now whatever you like to
}
}
如果您想获得索引并决定要获取哪个值,请使用常规for
循环使用索引并使用jim.get(index)
并使用split()
。
话虽如此,是什么阻止你在列表中循环?这是数组列表的常见用法 - 循环遍历它:)
答案 1 :(得分:0)
String jim_string = jim.toString().replace("[", "");
jim_string.replace("]", "");
String splitted[] = jim_string.split(",");
System.out.println(splitted[0]);
答案 2 :(得分:0)
put the below code lines into your code, it'll give your desire output.
int indx=0; //use the index you want to get & split
String tmp = jim.get(indx); // get the string from the ArrayList
if(tmp!=null && !tmp.equals("")){ // null check
for (String retval: tmp.split(";")){ // split & print
System.out.println(retval);
}
}