Saturday, December 28, 2024
Google search engine
HomeLanguagesJavaConvert an ArrayList of String to a String Array in Java

Convert an ArrayList of String to a String Array in Java

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]
Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments