我有以下清单:
1:成人(1 X 9,288)
2:儿童(1 X 9,288)
3:婴儿(1 X 2,429)
4:成人(1 X2,712)
5:儿童(1 X 2,712)
6:婴儿(1 X 146)
我需要从abve列表中提取信息,如下所示:
清单1:成人,儿童,婴儿
清单2:1,1,1,1,1,1
清单3:9,288,9,288,2,429.2,712,2,712,146
使用下面的代码,但给我错误:
HashMap<String, HashMap<String,String>>getPassengerList(){
HashMap<String, HashMap<String,String>> map = new HashMap<String, HashMap<String,String>>();
HashMap<String,String> map1 = new HashMap<String,String>();
//ArrayList<String> passlist = new ArrayList<>();
int size = paxList.size();
for(int i=0;i<size;i++){
//passlist.add(paxList.get(i).getText());
System.out.println(paxList.get(i).getText());
String pax = (paxList.get(i).getText().trim()).replaceAll("(", "");
String[] passType = pax.split(" ");
System.out.println(passType[0]+" ....... "+passType[1]);
String[] passCount = passType[1].split("X");
map1.put(passCount[0],passCount[1]);
map.put(passType[0],map1);
}
for(String name : map.keySet()){
String key = name.toString();
String value = map.get(name).toString();
System.out.println(key + " " + value);
}
return map;
}
最佳答案
import java.util.ArrayList;
import java.util.List;
/**
*
* @author jekink
*
*/
public class Test {
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("Adult(s) (1 X 9,288)");
list.add("Child(s) (1 X 9,288)");
list.add("Infant(s) (1 X 2,429)");
list.add("Adult(s) (1 X2,712)");
list.add("Child(s) (1 X 2,712)");
list.add("Infant(s) (1 X 146)");
List<String> list1= new ArrayList();
List<String> list2= new ArrayList();
List<String> list3= new ArrayList();
String first;
String second;
String third;
for(String value:list){
second=value.split("X")[0].split("\\(")[2];
first=value.split("\\)")[0]+")";
third=value.split("X")[1].trim().split("\\)")[0];
if(!list1.contains(first)){
list1.add(first);
}
list2.add(second);
list3.add(third);
}
System.out.println("List1 :"+list1);
System.out.println("List2 :"+list2);
System.out.println("List3 :"+list3);
}
}