
- 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 between List and Array in Java?
The List interface is a Collection and it stores a sequence of elements. ArrayList is the most popular implementation of the List interface. A list provides user has quite precise control over where an element to be inserted in the List. These elements are accessible by their index and are searchable. ArrayList is the most common implementation of List interface.
An array is a data structure which stores a fixed-size sequential collection of elements of the same type. An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type.
A List interface provides methods to convert a list to an array using toArray() methods. One of the toArray() method accepts another array of object which helps in converting a list to array of specific types. Otherwise we can use toArray() without parameter to get an array of Object. In this article, we're covering the toArray() methods of List with a complete examples to demonstrate the use of toArray() methods to get an array from list.
1. Use toArray() method without parameter.
Object[] toArray()
Notes
Returns an array containing all of the elements in this list in proper sequence (from first to last element).
The returned array will be "safe" in that no references to it are maintained by this list. (In other words, this method must allocate a new array even if this list is backed by an array). The caller is thus free to modify the returned array.
This method acts as bridge between array-based and collection-based APIs.
Returns
An array containing all of the elements in this list in proper sequence.
2. Use toArray() with array.
<T> T[] toArray(T[] a)
Notes
Returns an array containing all of the elements in this list in proper sequence (from first to last element).
The returned array will be "safe" in that no references to it are maintained by this list.
This method must allocate a new array even if this list is backed by an array).
The caller is thus free to modify the returned array.
This method acts as bridge between array-based and collection-based APIs.
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.
Arrays class provides a asList() method to get the list containing all the elements of an array.
public static <T> List<T> asList(T... a)
Type Parameter
T - the runtime type of the element.
Parameters
a - the array by which the list will be backed.
Returns
A list view of the specified array.
Example 1
Following is the example showing the usage of toArray() and Arrays.asList() 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) { String[] array = {"A", "B", "C"}; // array to List conversion List<String> list = new ArrayList<>(Arrays.asList(array)); System.out.println("List: " + list); // list to array conversion Object[] items = list.toArray(); for (Object object : items) { System.out.print(object + " "); } } }
Output
This will produce the following result −
List: [A, B, C] A B C
Example 2
Following is another example showing the usage of toArray() with specific typed array and Arrays.asList() 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) { String[] array = {"A", "B", "C"}; // array to List conversion List<String> list = new ArrayList<>(Arrays.asList(array)); System.out.println("List: " + list); // list to array conversion String[] strings = list.toArray(new String[0]); for (String string : strings) { System.out.print(string + " "); } } }
Output
This will produce the following result −
List: [A, B, C] A B C
- Related Articles
- convert list to array in java
- How to convert a list to array in Java?
- 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?
- How do you convert list to array in Java?
- How to convert an array to a list in Java?
- Program to convert Array to List in Java
- How to convert Wrapper value array list into primitive array in Java?
- How to convert an Array to a List in Java Program?
- Can we convert an array to list and back in Java?
- How can we convert a list to the JSON array in Java?
- Convert array to generic list with Java Reflections
- Can we convert a Java array to list?
