How to convert a List to an Array in Java

+1 vote
I am working on a List and I want to convert it into an Array. How can I do this?
May 10, 2018 in Java by Parth
• 4,630 points
215 views

3 answers to this question.

0 votes

If you want an array of primitive data type, then execute the code given below. 

List<Integer> list = ...;
int[] array = new int[list.size()];
for(int i = 0; i < list.size(); i++) array[i] = list.get(i);
answered May 10, 2018 by Daisy
• 8,110 points
0 votes
String[] strings = list.stream().toArray(String[]::new);
answered Jul 25, 2018 by Sushmita
• 6,890 points
0 votes

Either:

Foo[] array = list.toArray(new Foo[list.size()]);

or:

Foo[] array = new Foo[list.size()];
list.toArray(array); // fill the array
answered Aug 7, 2018 by Akrati
• 3,170 points

Related Questions In Java

0 votes
2 answers

How to convert array into list in Java?

Another workaround if you use apache commons-lang: int[] ...READ MORE

answered Aug 9, 2018 in Java by samarth295
• 2,200 points
179 views
0 votes
2 answers

How to convert an int array to string using tostring method in java?

Use java.util.Arrays: String res = Arrays.toString(array); System. ...READ MORE

answered Aug 16, 2019 in Java by Sirajul
• 58,990 points
1,166 views
0 votes
1 answer

How do I convert a String to an int in Java?

Hello @kartik, We can use the parseInt(String str) method of ...READ MORE

answered Jul 28, 2020 in Java by Niroj
• 82,580 points
139 views
0 votes
1 answer

How to convert a Set to List in Java?

Hi@MD, The most straightforward way to convert a ...READ MORE

answered Dec 30, 2020 in Java by akhtar
• 38,170 points
40 views
0 votes
1 answer

Are arrays equivalent to objects in Java ?

Yes; the Java Language Specification writes: In the Java ...READ MORE

answered May 10, 2018 in Java by Rishabh
• 3,620 points
278 views
0 votes
1 answer

Remove objects from an array in Java?

We can use external libraries: org.apache.commons.lang.ArrayUtils.remove(java.lang.Object[] array, int ...READ MORE

answered Jun 26, 2018 in Java by scarlett
• 1,270 points
261 views
0 votes
1 answer

Convert ArrayList<String> to String[] array

Use like this. List<String> stockList = new ArrayList<String>(); stockList.add("stock1"); stockList.add("stock2"); String[] ...READ MORE

answered Dec 22, 2020 in Java by Gitika
• 65,870 points
49 views
+1 vote
3 answers

What is the syntax to declare and initialize an array in java?

You can use this method: String[] strs = ...READ MORE

answered Jul 25, 2018 in Java by samarth295
• 2,200 points
2,047 views
0 votes
2 answers

How to create a 2-D array in java?

int[][] multi = new int[5][]; multi[0] = new ...READ MORE

answered Jul 16, 2018 in Java by Daisy
• 8,110 points
251 views
0 votes
2 answers