snippetjavaCritical
Convert list to array in Java
Viewed 0 times
arrayconvertlistjava
Problem
How can I convert a
Check the code below:
I need to populate the array
List to an Array in Java?Check the code below:
ArrayList tiendas;
List tiendasList;
tiendas = new ArrayList();
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.Solution
Either:
or:
Note that this works only for arrays of reference types. For arrays of primitive types, use the traditional way:
Update:
It is recommended now to use
From JetBrains Intellij Idea inspection:
There are two styles to convert a collection to an array: either using
a pre-sized array (like c.toArray(new String[c.size()])) or
using an empty array (like c.toArray(new String[0]).
In
older Java versions using pre-sized array was recommended, as the
reflection call which is necessary to create an array of proper size
was quite slow. However since late updates of OpenJDK 6 this call
was intrinsified, making the performance of the empty array version
the same and sometimes even better, compared to the pre-sized
version. Also passing pre-sized array is dangerous for a concurrent or
synchronized collection as a data race is possible between the
size and toArray call which may result in extra nulls
at the end of the array, if the collection was concurrently shrunk
during the operation.
This inspection allows to follow the
uniform style: either using an empty array (which is recommended in
modern Java) or using a pre-sized array (which might be faster in
older Java versions or non-HotSpot based JVMs).
Foo[] array = list.toArray(new Foo[0]);or:
Foo[] array = new Foo[list.size()];
list.toArray(array); // fill the arrayNote that this works only for arrays of reference types. For arrays of primitive types, use the traditional way:
List list = ...;
int[] array = new int[list.size()];
for(int i = 0; i < list.size(); i++) array[i] = list.get(i);Update:
It is recommended now to use
list.toArray(new Foo[0]);, not list.toArray(new Foo[list.size()]);.From JetBrains Intellij Idea inspection:
There are two styles to convert a collection to an array: either using
a pre-sized array (like c.toArray(new String[c.size()])) or
using an empty array (like c.toArray(new String[0]).
In
older Java versions using pre-sized array was recommended, as the
reflection call which is necessary to create an array of proper size
was quite slow. However since late updates of OpenJDK 6 this call
was intrinsified, making the performance of the empty array version
the same and sometimes even better, compared to the pre-sized
version. Also passing pre-sized array is dangerous for a concurrent or
synchronized collection as a data race is possible between the
size and toArray call which may result in extra nulls
at the end of the array, if the collection was concurrently shrunk
during the operation.
This inspection allows to follow the
uniform style: either using an empty array (which is recommended in
modern Java) or using a pre-sized array (which might be faster in
older Java versions or non-HotSpot based JVMs).
Code Snippets
Foo[] array = list.toArray(new Foo[0]);Foo[] array = new Foo[list.size()];
list.toArray(array); // fill the arrayList<Integer> list = ...;
int[] array = new int[list.size()];
for(int i = 0; i < list.size(); i++) array[i] = list.get(i);Context
Stack Overflow Q#9572795, score: 1514
Revisions (0)
No revisions yet.