打印arraylist的索引时如何删除最后一个逗号 [英] How to remove the last comma when I print out the index of arraylist
本文介绍了打印arraylist的索引时如何删除最后一个逗号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
List<String> list1 = new ArrayList<String>(words.length);
List<String> list2 = new ArrayList<String>(word.length);
for(String x: list1){
for(String y: list2){
if(x.equalsIgnoreCase(y)){
System.out.print(list1.indexOf(x) +" "+ ",");
}
}
}
对于这个函数,运行之后,输出将是 2,5,7....9,我的问题是如何删除最后一个逗号?
For this function, after run it, the output will be 2,5,7....9, My question is how to remove the last comma?
推荐答案
我会在开头打印逗号,像这样 -
I would print the comma at the start, something like this -
boolean first = true;
for(String x: list1){
for(String y: list2){
if(x.equalsIgnoreCase(y)){
if (first) {
first = false;
} else {
System.out.print(", "); // <-- or " ," if you prefer.
}
System.out.print(list1.indexOf(x));
}
}
}
这篇关于打印arraylist的索引时如何删除最后一个逗号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文