-2

Possible Duplicate:
Best way to convert an ArrayList to a string

I need your help! I have array of JSon (esponse from Facebook). ArrayList. How can convert this array to String? Thanks.

Community
  • 1
  • 1
yardo
  • 17
  • 1
  • 3

3 Answers3

1
StringBuilder sb=new StringBuilder();
for (Long l:list)
    sb.append(l);
Arneball
  • 359
  • 3
  • 10
0

best way would be to iterate over ArrayList and build a String

ArrayList<Long> list = new ArrayList<Long>(); 

String listString = ""; 

for (long l : list) 
{ 
    listString += String.valueOf(l) + "\n";
} 

UPDATE using StringBuilder is much better approach as Strings are immutable and everytime you append to String it creates new String in memory. also

"+" operator is overloaded for String and used to concatenated two String. Internally "+" operation is implemented using either StringBuffer or StringBuilder.

Mayank
  • 8,777
  • 4
  • 35
  • 60
0

Try using StringBuilder class

public String ConvertArrayToString(Long[] longArray)
{
    if(longArray != null)
    {
        StringBuilder sb = new StringBuilder(longArray.length);         
        for(int i = 0; i < longArray.length; i++)
        {
            sb.append(longArray[i]);
        }
        return sb.toString();
    }
    return null;
}
Jan Remunda
  • 7,840
  • 8
  • 51
  • 60