
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
How to convert a list to array in Java?
The List provides two methods to convert a List into 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.
<T> T[] toArray(T[] a)
Type Parameter
T − The runtime type of the array.
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,5)); 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 (int number : numbers) { System.out.print(number + " "); } } }
Output
This will produce the following result −
List: [1, 2, 3, 4, 5] 1 2 3 4 5 1 2 3 4 5
- Related Articles
- How to convert an array to a list in Java?
- How to convert an Array to a List in Java Program?
- How to convert Integer array list to integer array in Java?
- How to convert Integer array list to float array in Java?
- How to convert Float array list to float array in Java?
- How to convert Long array list to long array in Java?
- convert list to array in java
- How do you convert list to array in Java?
- How to convert between List and Array in Java?
- Program to convert Array to List in Java
- Java program to convert a list to an array
- Java program to convert an array to a list
- Can we convert a Java array to list?
- Can we convert a Java list to array?
- How to convert Wrapper value array list into primitive array in Java?

Advertisements