Open In App

Convert an ArrayList of String to a String Array in Java

Last Updated : 03 Dec, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

In Java, as we all know ArrayList class is derived from the List interface. Here we are given an ArrayList of strings and the task is to convert the ArrayList to a string array.

Illustration:

Input : ArrayList = [ "Geeks", "for", "Geeks" ]
Output: String[] = {"Geeks", "for", "Geeks"}
Input : ArrayList = [ "Jupiter", "Saturn", "Uranus", "Neptune", "Sun"]
Output: String[] = {"Jupiter", "Saturn", "Uranus", "Neptune", "Sun"}

Methods:

  1. Using get() method of ArrayList class
  2. Using toArray() method of ArrayList class
  3. Using copyOf() method of Arrays class

Method 1: Using ArrayList.get() Method of ArrayList class

Syntax: 

str[j] = a1.get(j)

Approach: 

  1. Get the ArrayList of Strings.
  2. Find the size of ArrayList using size() method, and Create a String Array of this size.
  3. Fetch each element of the ArrayList one by one using get() method.
  4. Assigned each element into String Array using assignment(=) operator.
  5. Printing string array.

Example:

Java




// Java Program to Convert ArrayList to Array
// using toArray() Method
 
// Importing required classes
import java.util.ArrayList;
import java.util.Arrays;
 
// Main class
class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
 
        // Creating an empty ArrayList of string type
        ArrayList<String> al = new ArrayList<String>();
 
        // Populating the ArrayList by custom elements
        al.add("Anshul Aggarwal");
        al.add("Mayank Solanki");
        al.add("Abhishek Kelenia");
        al.add("Vivek Gupta");
 
        String[] str = new String[al.size()];
 
        for (int i = 0; i < al.size(); i++) {
            str[i] = al.get(i);
        }
 
        // Printing using for each loop
        for (String k : str) {
            System.out.println(k);
        }
    }
}


Output

Anshul Aggarwal
Mayank Solanki
Abhishek Kelenia
Vivek Gupta

Method 2: Using toArray() method of ArrayList class 

Here we will be creating an object array to store elements received from ArrayList by creating an array of strings.

Syntax: 

Object[] arr = a1.toArray()
String str = (String)obj;

Approach: 

  1. Get the ArrayList of String.
  2. Convert ArrayList to Object array using toArray() method.
  3. Iterate and convert each element to the desired type using typecasting. Here it is converted to String type and added to the string array.
  4. Print the string array

Example:

Java




// Java Program to Convert ArrayList to Array
// using toArray() Method
 
// Importing required classes
import java.util.*;
 
// Main class
class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
 
        // Creating an empty ArrayList of string type
        ArrayList<String> al = new ArrayList<String>();
 
        // Populating the ArrayList by custom elements
        al.add("Anshul Aggarwal");
        al.add("Mayank Solanki");
        al.add("Abhishek Kelenia");
        al.add("Vivek Gupta");
 
        // Converting above List to array using toArray()
        // method and storing it in an string array
        String k[] = al.toArray(new String[al.size()]);
 
        // Iterating over above string array
        for (String str : k) {
 
            // Printing the elements in above array
            System.out.println(str);
        }
    }
}


Output

Anshul Aggarwal
Mayank Solanki
Abhishek Kelenia
Vivek Gupta

Method 3: Using copyOf() method of Arrays class 

Syntax: 

Object[] gfg = a1.toArray()
String[] str = Arrays.copyOf(gfg, gfg.length, String[].class);

 Approach: 

  1. Get the ArrayList of String.
  2. Convert ArrayList to Object array using toArray() method.
  3. Convert it to String Array using Arrays.copyOf() method.
  4. Print String Array.

Example:

Java




// Java Program to Convert ArrayList to Array
// using toArray() Method
 
// Importing required classes
import java.util.*;
 
// Main class
class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
 
        // Creating an empty ArrayList of string type
        ArrayList<String> al = new ArrayList<String>();
 
        // Populating the ArrayList by custom elements
        al.add("Anshul Aggarwal");
        al.add("Mayank Solanki");
        al.add("Abhishek Kelenia");
        al.add("Vivek Gupta");
 
        String[] answer = Arrays.copyOf(
            al.toArray(), al.size(), String[].class);
        System.out.println(Arrays.toString(answer));
    }
}


Output

[Anshul Aggarwal, Mayank Solanki, Abhishek Kelenia, Vivek Gupta]


Similar Reads

How to clone an ArrayList to another ArrayList in Java?
The clone() method of the ArrayList class is used to clone an ArrayList to another ArrayList in Java as it returns a shallow copy of its caller ArrayList. Syntax: public Object clone(); Return Value: This function returns a copy of the instance of Object. Below program illustrate the Java.util.ArrayList.clone() method: Example: Java Code // Java pr
2 min read
Copy Elements of One ArrayList to Another ArrayList in Java
It is the implementation class of List Interface. It allows duplicated objects/elements and as well as maintains the insertion order. You can get the element present inside the ArrayList by the index of it now you need to pass it into the getting (index) method. You can add the elements into ArrayList using the add() method. Syntax: ArrayList Initi
7 min read
How to Convert an ArrayList or an Array into a TreeSet in Java?
In Java, ArrayList allows random access because it works like an array. It means you can access any element by its index in O(1) at any time. A TreeSet is a sorted set implementation of the Set interface based on a TreeMap. It provides log(n) time complexity for basic operations like add, remove, and contains due to its tree implementation. In this
3 min read
How to Convert an ArrayList of Objects to a JSON Array in Java?
In Java, an ArrayList is a resizable array implementation of the List interface. It implements the List interface and is the most commonly used implementation of List. In this article, we will learn how to convert an ArrayList of objects to a JSON array in Java. Steps to Convert an ArrayList of Objects to a JSON Array in JavaBelow are the steps and
3 min read
Convert ArrayList to Comma Separated String in Java
ArrayList is a part of collection framework and is present in java.util package. It provides us with dynamic arrays in Java. In order to convert ArrayList to a comma-separated String, these are the approaches available in Java as listed and proposed below as follows: Earlier before Java 8 there were only standard methods available in order for this
6 min read
How to Convert a String to ArrayList in Java?
Converting String to ArrayList means each character of the string is added as a separator character element in the ArrayList. Example: Input: 0001 Output: 0 0 0 1 Input: Geeks Output: G e e k s We can easily convert String to ArrayList in Java using the split() method and regular expression. Parameters: regex - a delimiting regular expressionLimit
1 min read
How to Convert an ArrayList into a JSON String in Java?
In Java, an ArrayList is a resizable array implementation of the List interface. It implements the List interface and is the most commonly used implementation of List. In this article, we will learn how to convert an ArrayList into a JSON string in Java. Steps to convert an ArrayList into a JSON string in JavaBelow are the steps and implementation
2 min read
How to Convert a Comma-Separated String into an ArrayList in Java ?
In Java, a String is a sequence of characters. They are created using the String class and once a string is created, they are immutable which means that their value cannot be changed. In this article, we will be learning how to convert a comma-separated String into an ArrayList in Java. Syntax of ArrayList: ArrayList&lt;datatype&gt; name = new Arra
3 min read
Convert HashSet to a ArrayList in Java
ArrayList class is a resizable array, present in java.util package. The difference between an array and an ArrayList in Java, is that the size of an array cannot be modified (i.e. if you want to append/add or remove element(s) to/from an array, you have to create a new array. However, elements can be added/appended or removed from an ArrayList with
4 min read
Java Program to Convert ArrayList to LinkedList
Given an array list, your task is to write a program to convert the given array list to Linked List in Java. Examples: Input: ArrayList: [Geeks, forGeeks, A computer Portal] Output: LinkedList: [Geeks, forGeeks, A computer Portal] Input: ArrayList: [1, 2, 3, 4, 5] Output: LinkedList: [1, 2, 3, 4, 5] ArrayList - An ArrayList is a part of the collect
6 min read