Way To Convert An ArrayList to a string:
This post helps you to when u have an requirement like you have an list of string and u have to concatenate with some CSV(comma separated values ) or "/" or ","
Following is the code which full fills the above requirement.
import java.util.ArrayList;
import java.util.List;
public class StringConcatination {
public static void main(String[] args) {
//List of Strings we want to concatenate
List<String> list=new ArrayList<String>();
list.add("string1");
list.add("string2");
//The string builder used to construct the string
StringBuilder commaSepValueBuilder = new StringBuilder();
//Looping through the list
for ( int i = 0; i< list.size(); i++){
//append the value into the builder
commaSepValueBuilder.append(list.get(i));
//if the value is not the last element of the list
//then append the comma(,) as well
if ( i != list.size()-1){
commaSepValueBuilder.append(" / ");
}
}
System.out.println(commaSepValueBuilder.toString());
}
}
No comments:
Post a Comment