Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
Convert an ArrayList of String to a String Array in Java
Next article icon

Convert an ArrayList of String to a String Array in Java

Last Updated : 03 Dec, 2021
Comments
Improve
Suggest changes
Like Article
Like
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]

Next Article
Convert an ArrayList of String to a String Array in Java

R

rajput-ji
Improve
Article Tags :
  • Java
  • Technical Scripter
  • Technical Scripter 2018
  • Java-Collections
  • Java-ArrayList
  • Java-Array-Programs
  • Java-String-Programs
  • Java-List-Programs
Practice Tags :
  • Java
  • Java-Collections

Similar Reads

    Convert Set of String to Array of String in Java
    Given a Set of Strings, the task is to convert the Set into an Array of Strings in Java. Examples: Input: Set<String>: ["ForGeeks", "A Computer Portal", "Geeks"] Output: String[]: ["ForGeeks", "A Computer Portal", "Geeks"] Input: Set<String>: ["G", "e", "k", "s"] Output: String[]: ["G",
    6 min read
    Conversion of Array To ArrayList in Java
    Following methods can be used for converting Array To ArrayList: Method 1: Using Arrays.asList() method Syntax: public static List asList(T... a) // Returns a fixed-size List as of size of given array. // Element Type of List is of same as type of array element type. // It returns an List containing
    5 min read
    Convert a String to a List of Characters in Java
    In Java, to convert a string into a list of characters, we can use several methods depending on the requirements. In this article, we will learn how to convert a string to a list of characters in Java.Example:In this example, we will use the toCharArray() method to convert a String into a character
    3 min read
    How to Convert Character Array to String in Java?
    Strings are defined as an array of characters. The difference between a character array and a string is the string is terminated with a special character "\0". A character array can be converted to a string and vice versa. In this article, we will discuss how to convert a character array to a string
    4 min read
    How to sort an Array of Strings in Java
    Array Of StringsTo sort an array of strings in Java, we can use Arrays.sort() function. Java // A sample Java program to // sort an array of strings // in ascending and descending // orders using Arrays.sort(). import java.util.Arrays; import java.util.Collections; // Driver Class public class SortE
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences