【发布时间】:2014-01-27 03:01:00
【问题描述】:
我有一个字符串,我想从 Stringbuffer 中拆分出来并将其保存到对象的数组列表中。
我有一串看起来像这样的数据:
001>10102011>874837>Y>Y>TEST001>No\\002>11102011>8743834>Y>Y>null>No\\
\ 表示保存到一个新对象。 如何将此字符串保存到 List listsaveobj 中?
我的示例保存对象是
public class SaveObject{
private String obj1;
private String obj2;
private String obj3;
private String obj4;
private String obj5;
private String obj6;
private String obj7;
//getters and setters
}
我被困住了
StringTokenizer st= new StringTokenizer(sb1.toString(),"\\\\");
int count = 0;
int total= st.countTokens();//count no of rows of data
System.out.println(total);
while(count<total){
while(st.hasMoreElements()){
tmpList.add(st.nextToken());
count++;
}
}
System.out.println(tmpList.size());
for(int j = 0; j<tmpList.size(); j++){
String str = tmpList.get(j);
StringTokenizer st2 = new StringTokenizer(str, ">");
while (st2.hasMoreElements()){
System.out.println(st2.nextToken());
// is it possible to save into ArrayList of objects here?
// I can see that the String can split into
// 001
// 10102011
// 874873
// Y
// Y
// TEST001
// No etc
/* using st.nextToken but I don't know how to continue from here. Pls help!! Thanks. I want to do something like
obj = new Object();
obj.saveObj1(...);
obj.saveObj2(...);
obj.saveObj3(...);
obj.saveObj4(...);
obj.saveObj5(...);
obj.saveObj6(...);
obj.saveObj7(...);
listsaveobj.add(obj); but it's not possible? */
}
}
【问题讨论】:
-
你会考虑用数组替换这七个字段吗?
-
嗨,大卫,是的,当然:)
-
你在分裂什么?
标签: java string arraylist stringtokenizer