Convert a java arraylist to comma delimited string

Is there a fast way to convert a java arraylist to comma delimited string without using an Iterator?
lasuttonAsked:
Who is Participating?
 
rrzConnect With a Mentor Commented:
You could use
String  listStringWithBrackets = myList.toString();
and then strip the brackets.
0
 
lasuttonAuthor Commented:
Thanks!

String myString= myArrayList.toString();
myString= myString.substring(1,myString.length()-1);
       
0
 
brunoguimaraesCommented:
Actually the toString method will not return the arraylist's values, unless you override it.

I think you'll have to iterate through the arraylist, but you can use the enhanced for loop (if you're using JDK 5 or greater):
StringBuilder sb = new StringBuilder();
int count = 0;
 
for (String s : yourArray) {
     sb.append(s);
     if (++count != yourArray.length) {
         sb.append(",");
     }
}
 
return sb.toString();

Open in new window

0
 
Ajay-SinghCommented:
> Is there a fast way to convert a java arraylist to comma delimited string without using an Iterator?
NO. You need to traverse through the list once to produce the string output. toString method does the same.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.