- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Can we convert a list to an Array in Java?
We can convert a list into an array easily using its toArray() method.
Syntax
<T> T[] toArray(T[] a)
Returns an array containing all of the elements in this list in proper sequence (from first to last element); the runtime type of the returned array is that of the specified array. If the list fits in the specified array, it is returned therein. Otherwise, a new array is allocated with the runtime type of the specified array and the size of this list.
Type Parameter
T − The runtime type of an array.
Returns
An array containing all of the elements in this list in proper sequence.
Throws
ArrayStoreException − If the runtime type of the specified array is not a supertype of the runtime type of every element in this list.
NullPointerException − If the specified array is null.
Example
Following example shows how to convert a list into array.
package com.tutorialspoint; import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class CollectionsDemo { public static void main(String[] args) { // Create a list object List<String> list = new ArrayList<>(Arrays.asList("A", "B", "C")); // print the list System.out.println(list); String[] array = list.toArray(new String[0]); for (String string : array) { System.out.print(string + " "); } } }
Output
This will produce the following result −
[A, B, C] A B C
- Related Articles
- Can we convert a Java array to list?
- Can we convert a Java list to array?
- Can we convert an array to list and back in Java?
- How can we convert a list to the JSON array in Java?
- How can we convert a JSON array to a list using Jackson in Java?
- Can we convert a list to a Set in Java?
- How can we convert list to Set in Java?
- Can we convert a List to Set and back in Java?
- How to convert an array to a list in Java?
- Java program to convert a list to an array
- Java program to convert an array to a list
- How can we convert a JSONArray to String Array in Java?
- How can we convert character array to a Reader in Java?
- How to convert an Array to a List in Java Program?
- convert list to array in java
