Solved

Convert a java arraylist to comma delimited string

Posted on 2008-06-13
4
7,957 Views
Last Modified: 2008-06-13
Is there a fast way to convert a java arraylist to comma delimited string without using an Iterator?
0
Comment
Question by:lasutton
4 Comments
 
LVL 27

Accepted Solution

by:
rrz earned 500 total points
ID: 21779141
You could use
String  listStringWithBrackets = myList.toString();
and then strip the brackets.
0
 

Author Comment

by:lasutton
ID: 21779228
Thanks!

String myString= myArrayList.toString();
myString= myString.substring(1,myString.length()-1);
       
0
 
LVL 9

Expert Comment

by:brunoguimaraes
ID: 21779256
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
 
LVL 23

Expert Comment

by:Ajay-Singh
ID: 21779300
> 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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Java. Convert method from recursion based to iteration based ( loop based ) 6 76
compre toata in where clue oracle 4 57
ForLoop Example 3 40
factorial example 4 35
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now