This article is participating in the Java Theme Month – Java Debug Notes Event, see the event link for details

Convert lists to arrays in Java

How can I convert List to ArrayJava?

Check the following code:

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);  
Copy the code

I need the values used by Tiendas to populate the array tiendasList.

Answer:


A lot of knowledge points, really need to write out will master ! ! !   \color{purple} a lot of knowledge points, really need to write out just can master!! {~}

Any of them:

Foo[] array = list.toArray(new Foo[0]);
Copy the code

Or:

Foo[] array = new Foo[list.size()];
list.toArray(array); // fill the array
Copy the code

Note that this only applies to arrays of reference type. For primitive arrays, use the traditional approach:

List<Integer> list = ... ; int[] array = new int[list.size()]; for(int i = 0; i < list.size(); i++) array[i] = list.get(i);Copy the code

Update:

ToArray (new Foo[0]); Instead of list.toarray (new Foo[list.size()]); .

Check from JetBrains Intellij Idea:

There are two styles for converting collections to arrays: using a pre-sized array (such as C.toarray (new String [c.size ()])) or using an empty array (such as C.Toarray (new String [0]).

In earlier Versions of Java, pre-sized arrays were recommended because the reflection calls required to create an appropriately sized array were very slow. However, because OpenJDK 6 is newer, this call becomes interesting, and the empty array version performs the same, and sometimes better, than the empty size version. In addition, passing arrays of predefined sizes can be dangerous for concurrent or synchronized collections, because data contention can occur between size and toArray calls, and if the collection is simultaneously shrunk during the operation, additional null values can result at the end of the array.

This check allows a uniform pattern to be followed: use empty arrays (recommended in modern Java) or use arrays of predetermined sizes (which may be faster in older Java versions or non-Hotspot based JVMS).

Answer:

Alternatives in Java 8:

String[] strings = list.stream().toArray(String[]::new);
Copy the code

Starting with Java 11:

String[] strings = list.toArray(String[]::new);
Copy the code

The article translated from kgs4h5t57thfb6iyuz6dqtun5y ac4c6men2g7xr2a – stackoverflow – com. Translate. Goog/questions / 9…

String[] strings = list.stream().toarray (String[]::new)


Welcome to my column S t a c k O v e r F l o w . I select the best questions and answers and test them frequently in interviews ! ! !   \color{red} Welcome to my column StackOverFlow, I will filter the quality of the interview test!! {~}


There are the latest and elegant ways to do this, and I will write my thoughts on this q&A at the end of the article \color{red} has the latest, elegant implementation, and I will also write my opinion on this question at the end of the article {~}

Thank you for reading this, if this article is well written and if you feel there is something to it

Ask for a thumbs up 👍 ask for attention ❤️ ask for share 👥 for 8 abs I really very useful!!

If there are any mistakes in this blog, please comment, thank you very much! ❤ ️ ❤ ️ ❤ ️ ❤ ️