

- 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
Can we convert a Java list to array?
<p>The List provides two methods to convert a List into Array.</p><h2>1. Use toArray() method without parameter.</h2><pre class="result notranslate">Object[] toArray()</pre><h3>Returns</h3><p>An array containing all of the elements in this list in proper sequence.</p><h2>2. Use toArray() with array.</h2><pre class="result notranslate"><T> T[] toArray(T[] a)</pre><h3>Parameters</h3><ul class="list"><li><p><strong>a </strong> − 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.</p></li></ul><h3>Returns</h3><p>An array containing the elements of this list.</p><h3>Throws</h3><ul class="list"><li><p><strong>ArrayStoreException</strong> − If the runtime type of the specified array is not a supertype of the runtime type of every element in this list.</p></li><li><p><strong>NullPointerException </strong>− If the specified array is null.</p></li></ul><h2>Example</h2><p>Following is the example showing the usage of toArray() methods −</p><pre class="demo-code notranslate language-java" data-lang="java">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[0]); for (String string : characters) { System.out.print(string + " "); } } }</pre><h2>Output</h2><p>This will produce the following result −</p><pre class="result notranslate">List: [A, B, C, D] A B C D A B C D</pre>
- Related Questions & Answers
- Can we convert a Java array to list?
- Can we convert a list to an Array in Java?
- How can we convert a list to the JSON array in Java?
- Can we convert an array to list and back in Java?
- Can we convert a list to a Set in Java?
- How can we convert a JSON array to a list using Jackson in Java?
- How can we convert list to Set in Java?
- Can we convert a List to Set and back in Java?
- How can we convert character array to a Reader in Java?
- How can we convert a JSONArray to String Array in Java?
- convert list to array in java
- Java program to convert a list to an array
- Java program to convert an array to a list
- How to convert a list to array in Java?
- In how many ways we can convert a String to a character array using Java?
Advertisements