Table of contents
1.
Introduction
2.
What is an Array?
3.
Difference between List and Array in Java
4.
Methods to Convert List to array in Java
4.1.
Method 1: Using get( ) method
4.2.
Java
4.3.
Method 2: Using toArray( ) method
4.4.
Java
4.5.
Method 3: Using Stream introduced in java8
4.6.
Java
5.
Algorithm to Convert a List to Array in Java
6.
Frequently Asked Questions
6.1.
How do you convert a list to an array in Java?
6.2.
Why toArray( ) method is used in Java?
6.3.
Can you convert an ArrayList to an array?
6.4.
Can we convert a list to a String array in Java?
7.
Conclusion
Last Updated: Sep 30, 2024
Easy

Convert list to array in Java

Author Tisha
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Convert list to array in Java is a common operation that can be done in various ways. This article explores multiple methods for converting a list into an array, while also explaining key differences between lists and arrays.

In this article, we will learn how to convert list to Array in Java. There are multiple ways to convert list to array in Java, and we will learn all of them one by one. First, we will see what an array is and the methods to convert list to an array. Then we will see the difference between the array and the list.

Introduction

What is an Array?

A group of the same types of elements that can be accessed individually using an index with the identifier is called an array. It stores various values in a single variable; the difference between the two indexes is the offset. In this, the length of an array is placed when the array is created. After creation, its extent is stable. The first index value in the array is zero. Arrays could have one or two dimensions. Arrays in Java can hold primitive data types(Character, Float, Integer, etc.) and non-primitive data types(String array, Interface, etc.). You can also see here the difference between primitive and non-primitive datatypes. An array is permanently stored in consecutive(specific) memory locations.

Also see,  Swap Function in Java

Difference between List and Array in Java

Points of ComparisonListArray

Size 

The size of a list is Dynamic. Its size may not specify while listening.

The size of an array is Static. Its size must specify while installing.

Performance

In Java, there is a lower performance in the list.

In Java, there is higher performance in an array because of its fixed size.

  Storage Capability

It can store only objects.

It can store both objects and primitive types.

Iteration

It required the iterator.

It must use for or for each.

Capacity

size() method is used to find out the capacity.

length() method is used to find out the capacity.

  Add Elements

To add the element, add( )method is used.

To add the element, assignment operators are used.

Multi-Dimensional

No, it is not multi-dimensional.

Yes, it is multi-dimensional

Methods to Convert List to array in Java

Here, we will learn about different methods that we can use to convert list to array in java. There are three methods that we can use. These are:

  1.  Using get( ) method
  2.  Using toArray( ) method
  3.  Using Stream introduced in java8

Method 1: Using get( ) method

The method returns the element at a given index from the specified array. It is an inbuilt method in Java. We can use below this method to convert list to array in java with the help of an example.

Syntax: 

public E get(int index)

Example:

  • Java

Java

// program to Convert List to Array in java
import java.io.*;
import java. util.LinkedList;
import java.util.List;
class codingninjas {
   // Main method
   public static void main(String[] args)
   {
       List<String> list = new LinkedList<String>();
       list.add("coding");
       list.add("ninjas");
       list.add("Coding Ninjas Studio");
       list.add("Practice");  
       String[] arr = new String[list.size()];
       for (int i = 0; i < list.size(); i++)
           arr[i] = list.get(i);
       for (String x: arr)
           System.out.print(x + " ");
   }
}
You can also try this code with Online Java Compiler
Run Code

Output: 
 

Output 1

Method 2: Using toArray( ) method

This method has to use a toArray( ) form in Java. This method is used to return all the elements in the proper sequence. We can use below this method with the help of an example.

Syntax:

public Object[] toArray()

           or

public <T> T[] toArray(T[] a)

Example:

  • Java

Java

// program to Convert List to Array in java
import java.util.*;
public class Codingninjas {
  // Main method
   public static void main(String[] args)
   {
       List<String> list = new LinkedList<String>();
       list.add("coding");
       list.add("ninjas");
       list.add("code");
       list.add("Practice");
       String[] arr = list.toArray(new String[0]);
       for (String x: arr)
           System.out.print(x + " ");
   }
}
You can also try this code with Online Java Compiler
Run Code

Output: 

Output 2

 You can compile java code just by clicking here.

Method 3: Using Stream introduced in java8

In this method, the stream API is used to process collections of objects. We can use this method to convert list to array in java.

Example:

  • Java

Java

// program to Convert List to Array in java
import java.util.*;
class codingninjas {
   // Main method
   public static void main(String[] args)
   {
       List<String> list = new LinkedList<String>();     
       list.add("coding");
       list.add("ninjas");
       list.add("code");
       list.add("Practice");
      // Storing size of List
       int n = list.size();
   String[] arr = list.stream().toArray(String[] ::new);
          for (String x : arr)
           System.out.print(x + " ");
   }
}
You can also try this code with Online Java Compiler
Run Code

Output: 

Output 3


You can also find the output of this java compiler code here.

Algorithm to Convert a List to Array in Java

  1. Create a List: Begin by having a list containing elements.
  2. Specify Array Type: Determine the type of array that will be created based on the type of elements in the list.
  3. Allocate Array: Allocate an array of appropriate size to hold all elements from the list.
  4. Copy Elements: Iterate through the list and copy each element to the corresponding position in the array.
  5. Return Array: Once all elements are copied, return the array containing the elements from the list.

Must Read Difference between ArrayList and LinkedListDuck Number in Java

Frequently Asked Questions

How do you convert a list to an array in Java?

Using the .toArray( )method is the easiest and best way to convert a list into an array.

Why toArray( ) method is used in Java?

This method can be used to return an array containing all the elements in ArrayList in the correct order.

Can you convert an ArrayList to an array?

We can use the toArray() method to convert the Arraylist to an array in Java. 

Can we convert a list to a String array in Java?

Yes, we can convert a list to a string array in java by converting ArrayList to an Object array by using the toArray( )method. Here the list converts into string type and is added to the string array.

Conclusion

In this article, we have learned about how to convert list to array in java and the difference between both functions. We have also seen the programming and some examples which convert the list into an array.

Recommended problems -

Refer to our guided path on code360 to learn more about DSA Competitive Programming, Javascript System Design, etc. Enroll in our courses and refer to the mock test and problems available. Also, look at the interview experiences for placement preparations.

Live masterclass