Reputation: 195
I am attempting to add a comma separator to the String representation of a Java ArrayList but nothing seems to work, I am obviously missing something simple.
Sadly old Java 6 (running in a JSP):
ArrayList zones= new ArrayList();
zones.add(pageContext.getAttribute("zone"));
for(int i=0; i<zones.size(); i++)
out.println(zones.get(i));
// output is CellA116 CellA116 CellA116 Reception Reception CellA11
StringBuffer stringBuffer = new StringBuffer();
for(int i=0; i<zones.size(); i++)
{
stringBuffer.append(zones.get(i));
stringBuffer.append(",");
}
out.println(stringBuffer.toString());
// output is CellA116,CellA116,CellA116,Reception,Reception,CellA11, (commas)
%>
</tr>
</c:forEach>
syntax wont work here (outside of loop)
out.println(stringBuffer.substring(0, stringBuffer.length() - 1));
I need to remove the final comma (as I eventually want to use the array in chart.js), appreciate any thoughts.
Upvotes: 1
Views: 1617
Reputation: 10562
Generally speaking, relying on a toString()
method to do this is an easy way to inadvertently introduce bugs later on. If you change what concrete class is providing the collection (maybe to a Set
instead of a List
for example), your assumption that it starts and ends with square brackets might be untrue, and your output might change without you realising it.
I'd suggest that a more appropriate solution would be to iterate over the collection of Strings and add them to a StringBuilder
.
So, it might look something like:
StringBuilder stringBuilder = new StringBuilder();
for(int i=0; i<strList.size(); i++)
{
stringBuilder.append(strList.get(i));
stringBuilder.append(",");
}
// Remove the last character from the StringBuilder to avoid a trailing comma.
String commaSeparatedList = stringBuilder.substring(0, stringBuilder.length() - 1);
out.println(commaSeparatedList);
Upvotes: 2
Reputation: 18568
You can utilize a StringBuilder
and a classic for
loop to output a csv line from List<String>
.
Have a look at this:
public static void main(String args[]) {
List<String> zones = new ArrayList<String>();
zones.add("CellA116");
zones.add("CellA116");
zones.add("CellA116");
zones.add("Reception");
zones.add("Reception");
zones.add("CellA11");
StringBuilder sb = new StringBuilder();
for (int i = 0; i < zones.size(); i++) {
if (i < zones.size() - 1) {
sb.append(zones.get(i)).append(";");
} else {
sb.append(zones.get(i));
}
}
System.out.println(sb.toString());
}
The output will look like this:
CellA116;CellA116;CellA116;Reception;Reception;CellA11
If you want to have a method with a flexible separator char
, then use something like this:
public static String toSeparatedString(List<String> values, char separator) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < values.size(); i++) {
if (i < values.size() - 1) {
sb.append(values.get(i)).append(separator);
} else {
sb.append(values.get(i));
}
}
return sb.toString();
}
Upvotes: 2
Reputation: 129
I dont see the need for another for loop to replace the [] and , zones.toString().replaceAll("[\[\]\s]", "") suggested by Lino works well.
Upvotes: -2