- 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
How do I turn a list into an array in Java?
We can use toArray() methods of List to convert them to an array.
1. Use toArray() method without parameter.
Object[] toArray()
Returns
An array containing all of the elements in this list in proper sequence.
2. Use toArray() with array of specific type of elements.
<T> T[] toArray(T[] a)
Parameters
a − The array into which the elements of this list are to be stored, if it is big enough; otherwise, a new array of the same runtime type is allocated for this purpose.
Returns
An array containing the elements of this list.
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 is the example showing the usage of toArray() methods −
package com.tutorialspoint; import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class CollectionsDemo { public static void main(String[] args) { List<Integer> list = new ArrayList<>(Arrays.asList(1,2,3,4)); System.out.println("List: " + list); Object[] items = list.toArray(); for (Object object : items) { System.out.print(object + " "); } System.out.println(); Integer[] numbers = list.toArray(new Integer[0]); for (Integer number : numbers) { System.out.print(number + " "); } } }
Output
This will produce the following result −
List: [1, 2, 3, 4] 1 2 3 4 1 2 3 4
- Related Articles
- How do you turn a list into a Set in Java?
- How do you turn an ArrayList into a Set in Java?
- How do I add an element to an array list in Java?
- How to turn JavaScript array into the comma-separated list?
- How do you convert a list collection into an array in C#?
- How do I find an element in Java List?
- How do I reverse an int array in Java
- How do I empty a list in Java?
- How do I search a list in Java?
- How do I insert all elements from one list into another in Java?
- How do I turn a string in dot notation into a nested object with a value – JavaScript?
- How do I declare and initialize an array in Java?
- How to change array into a list in Java?
- How do I insert elements in a Java list?
- How do I search through an array using a string, which is split into an array with JavaScript?

Advertisements