static void nodes(String node) {
int node_location;
int i;
int update_i=1;
node_location=((node.indexOf("(("))-2);
ArrayList<String> node_array = new ArrayList<String>();
for( i=1;i<node_location;i++) {
if(node.charAt(i)!=',') {
if(node.charAt(i+1)==',' || node.charAt(i+1)==')')
node_array.add(Character.toString(node.charAt(i)));
else {
for(int a=i+2;a<=node.indexOf("),");a++) {
update_i++;
if(node.charAt(a)==',') {
node_array.add(node.substring(i, a));
break;
}
}
i=update_i;
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
该方法应该采用格式的字符串(1,2,3,4,5)并将数字(作为字符串)存储在arraylist中.问题是当我有(1,2,333,4,5)例如我的if语句应该转到else并检查数字在我们到达逗号之前有多少个位置.然后我把它的子串并存储到我的arraylist中.问题是,由于某些原因,当我们到达else语句时,我的位置不会更新(我们必须增加比for循环更多的东西,因为数字不止一个.但是当我运行它时,我的程序打印出以下内容:
1
2
333
333
33
3
4
5
不幸的是,很难理解你的代码和你的问题 - 但这看起来很相关:
for(int a=i+2;a<=node.indexOf("),");a++)
Run Code Online (Sandbox Code Playgroud)
鉴于您的原始字符串根本不包含"),",indexOf将返回-1,并且您永远不会进入循环体.因此update_i永远不会增加,你会i回到1.
我强烈建议你完全重写你的代码 - 目前它看起来太复杂了它的实现.你不能拆分,,然后从每个字符串中删除任何非数字字符?
| 归档时间: |
|
| 查看次数: |
913 次 |
| 最近记录: |