I have a ArrayList that contains values in the form [ann,john]. I want to convert this ArrayList into a String array in the form {\"ann\",\"john\"}.
How should I do
You can try this code
String[] stringA = new String[stringArrayList.size()];
stringArrayList.toArray(stringA)
System.out.println(stringA[0]);
Use the method "toArray()"
ArrayList<String> mStringList= new ArrayList<String>();
mStringList.add("ann");
mStringList.add("john");
Object[] mStringArray = mStringList.toArray();
for(int i = 0; i < mStringArray.length ; i++){
Log.d("string is",(String)mStringArray[i]);
}
or you can do it like this: (mentioned in other answers)
ArrayList<String> mStringList= new ArrayList<String>();
mStringList.add("ann");
mStringList.add("john");
String[] mStringArray = new String[mStringList.size()];
mStringArray = mStringList.toArray(mStringArray);
for(int i = 0; i < mStringArray.length ; i++){
Log.d("string is",(String)mStringArray[i]);
}
http://developer.android.com/reference/java/util/ArrayList.html#toArray()
String[] array = new String[items2.size()];
items2.toArray(array);
You could make an array the same size as the ArrayList
and then make an iterated for loop to index the items and insert them into the array.
I know im very late to answer this. But it will help others, I was also stucked on this.
Here is what i did to get it work.
String[] namesArr = (String[]) names.toArray(new String[names.size()]);
try this
List<String> list = new ArrayList<String>();
list.add("List1");
list.add("List2");
String[] listArr = new String[list.size()];
listArr = list.toArray(listArr );
for(String s : listArr )
System.out.println(s);