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,640 points
1,137 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,140 points
0 votes
String[] strings = list.stream().toArray(String[]::new);
answered Jul 25, 2018 by Sushmita
• 6,920 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,190 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 10, 2018 in Java by samarth295
• 2,220 points
888 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
• 59,230 points
2,398 views
0 votes
2 answers

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

Use the lines of code mentioned below:- String ...READ MORE

answered Feb 9, 2022 in Java by Soham
• 9,710 points
3,209 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,260 points

edited Jun 22, 2023 by Khan Sarfaraz 614 views
+1 vote
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
1,326 views
+1 vote
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,290 points
1,185 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,770 points
1,282 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,220 points
3,578 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,140 points
1,544 views
0 votes
2 answers
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP