

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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 you convert an ArrayList to an array in Java?
An ArrayList provides two methods to convert it into Array.
Way #1
Object[] toArray()
Returns an array containing all of the elements in this list in proper sequence (from first to last element).
Way #2
<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 the array to contain the collection.
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<String> list = new ArrayList<>(Arrays.asList("A","B","C", "D")); System.out.println("List: " + list); Object[] items = list.toArray(); for (Object object : items) { System.out.print(object + " "); } System.out.println(); String[] characters = list.toArray(new String[list.size()]); for (String string : characters) { System.out.print(string + " "); } } }
Output
This will produce the following result −
List: [A, B, C, D] A B C D A B C D
- Related Questions & Answers
- How do you search for an element in an ArrayList in Java?
- Convert an ArrayList to an Array with zero length Array in Java
- How do you turn an ArrayList into a Set in Java?
- Convert an ArrayList to HashSet in Java
- How do you convert list to array in Java?
- How to create an ArrayList from an Array in Java?
- Convert an ArrayList of String to a String array in Java
- How many ways are there to convert an Array to ArrayList in Java?
- How do you convert a list collection into an array in C#?
- How do you empty an array in C#?
- How do you print the content of an array in Java?
- How to convert an object array to an integer array in Java?
- How do you perform an AND query on an array in MongoDB?
- How to convert an ArrayList to String in Kotlin?
- How do you create an empty list in Java?