Home Tutorial Java Collections Arraylist Java arraylist to comma separated string

 
 

Share on Google+Share on Google+
Java arraylist to comma separated string
Posted on: October 23, 2009 at 12:00 AM
Advertisement
This section demonstrates how ArrayList can be converted into comma separated string

  • arrayList is converted into the comma separated string.
  • For this take one by one element from the arrayList.
  • Then add it to the String array.


Example of Java Arraylist Comma Separated String
import java.util.ArrayList;

public class List1 {
    public static void main(String[] args) {
     int ar[]={1,2,3,4};
     ArrayList list=new ArrayList();
     list.add(ar[0]);
     list.add(ar[1]);
     list.add(ar[2]);
     list.add(ar[3]);

     String a=new String();

     for(int p:ar)
     {
      Integer x=new Integer(p);
      a=a+new Integer(p).toString()+",";
     }
     System.out.println(a);
  }
}
Output
1,2,3,4

Advertisement

Related Tags for Java arraylist to comma separated string:


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: October 23, 2009

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com