Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Convert a java arraylist to comma delimited string

Posted on 2008-06-13
4
Medium Priority
?
7,977 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 28

Accepted Solution

by:
rrz earned 1500 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

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…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

705 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