Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (2.6k points)

How can I convert an ArrayList<String> object to a String[] array in Java?

1 Answer

0 votes
by (46k points)

To convert an ArrayList<String> object to a String[] array in Java use toArray() method without passing any argument renders Object. So you have to pass an array as an argument, which will be packed with the data from the list, and returned. You can pass an empty array too, but you can also pass an array with the wanted size.

Syntax:

List<String> list = ..;

String[] array = list.toArray(new String[0]);

Example:

ArrayList<String> list = new ArrayList<String>();

list.add("lion");

list.add("king");

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

 

Related questions

0 votes
1 answer
0 votes
1 answer
asked Jul 27, 2019 in Java by Ritik (3.5k points)
0 votes
1 answer
+1 vote
2 answers
0 votes
1 answer

Browse Categories

...