Solved

Convert a java arraylist to comma delimited string

Posted on 2008-06-13
4
7,966 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 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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Desingning Refactoring existing code 2 31
JavaFX TableView not displaying correctly 3 103
Java program running SQL query 5 47
Eclipse for Java EE development 2 43
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 different types of Android Layout and some basics of an Android App.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

740 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