【发布时间】:2017-06-08 10:54:51
【问题描述】:
我正在使用类似这样的字符串值字符串结果"aa,bb,cc,dd,ee"。
并且已经将其拆分为aa bb cc dd ee
qrList = result.getContents().toString().split("\\,");
List<String> resultToString= new ArrayList(Arrays.asList(qrList));
然后我创建了四个 ArrayList。
ArrayList<String> strA = new ArrayList();
ArrayList<String> strB = new ArrayList();
ArrayList<String> strCD = new ArrayList();
ArrayList<String> strE = new ArrayList();
当我使用下面的代码将字符串存储到每个新的ArrayList 中时。
for(int count=0; count<resultToString.size(); count++){
//separate string to array list
if(count%5==0){
strA.add(resultToString.get(count));
}else if(count%5==1){
strB.add(resultToString.get(count));
}else if(count%5==2||count%5==3){
strCD.add(resultToString.get(count));
}else if(count==4){
strE.add(resultToString.get(count));
}
正确的结果是
- strA 存储 aa
- strB 存储 bb
- strCD 存储 cc 和 dd
- strE 存储 ee
它不起作用,因为我只获得了0 的索引值(strA 存储了 aa)。
我应该怎么做才能改进我的代码?
【问题讨论】:
-
如果你format your code first就太好了
-
你分工好吗?我手动尝试过,例如 "aa,bb,cc,dd,ee".toString().split("\\,");然后你的代码就可以正常工作了。
-
你说的不可能。
count==4对于您的给定输入必须为真 -
对于 motis10,拆分方法工作正常。
标签: java android string arraylist