This question already has an answer here:
How can I convert a List
to an Array
in Java?
Check the code below:
ArrayList<Tienda> tiendas;
List<Tienda> tiendasList;
tiendas = new ArrayList<Tienda>();
Resources res = this.getBaseContext().getResources();
XMLParser saxparser = new XMLParser(marca,res);
tiendasList = saxparser.parse(marca,res);
tiendas = tiendasList.toArray();
this.adaptador = new adaptadorMarca(this, R.layout.filamarca, tiendas);
setListAdapter(this.adaptador);
I need to populate the array tiendas
with the values of tiendasList
.
All collection implementations have an overloaded constructor that takes another collection (with the template
<T>
matching). The new instance is instantiated with the passed collection.Example taken from this page: http://www.java-examples.com/copy-all-elements-java-arraylist-object-array-example
For ArrayList the following works:
This (Ondrej's answer):
Is the most common idiom I see. Those who are suggesting that you use the actual list size instead of "0" are misunderstanding what's happening here. The toArray call does not care about the size or contents of the given array - it only needs its type. It would have been better if it took an actual Type in which case "Foo.class" would have been a lot clearer. Yes, this idiom generates a dummy object, but including the list size just means that you generate a larger dummy object. Again, the object is not used in any way; it's only the type that's needed.
I came across this code snippet that solves it.
The conversion works as follows:
Try this: