Convert ArrayList to String array in Java with Example

In this tutorial, I will be sharing how to convert an ArrayList to String array. There are three ways to achieve our goal:

1. Using ArrayList class get() method

2. Using ArrayList class toArray() method

3. Using Arrays class copyOf() method

Read Also :  Difference between ArrayList and Array in Java

Let's dive deep into the topic:

Convert ArrayList to String array

Method1 : Using ArrayList class get() method


In this example we have converted the whole list to array in three steps

a. Obtain the ArrayList size using size() method
b. Fetch each element of the list using get() method
c. Assigned each element to corresponding array element using assignment = operator
d. Print String array


import java.util.*;

public class ConvertArrayListToArray {
    public static void main(String args[]) {
      // Creating and initializing ArrayList
      ArrayList<String> fruits = new ArrayList<>();
      fruits.add("Apple");
      fruits.add("Banana");
      fruits.add("Mango");
      fruits.add("Pear");

      // ArrayList to String array conversion
      String[] str = new String[fruits.size()];
      for(int i=0; i < fruits.size(); i++) {
          str[i] = fruits.get(i);
      }
      
      // Print elements using for-each loop
      for(String s : str) {
        System.out.println(s);      
      }
    }
}


Output:

Apple
Banana
Mango
Pear

Method 2 : Using ArrayList class toArray() method


In this example we will convert ArrayList to String array using toArray() method.

a. Using toArray() method convert ArrayList to Object array.
b. Iterate each element and convert them to the desired type using typecasting. Here we are converting to String type and added to the String array.
c. Last step is to print the String array.


import java.util.*;

public class ConvertArrayListToArray2 {
    public static void main(String args[]) {
      // Instantiating and initializing ArrayList
      ArrayList<String> cities = new ArrayList<>();
      cities.add("Boston");
      cities.add("Dallas");
      cities.add("San jose");
      cities.add("Chicago");

      // ArrayList to String array conversion using toArray()
      String citinames[]=cities.toArray(new String[cities.size()]);

      // Printing elements using for-each loop
      for(String str : citinames) {
        System.out.println(str);      
      }
    }
}


Output:

Boston
Dallas
San jose
Chicago

Method3 : Using Arrays class copyOf() method


In this example, we will convert the ArrayList to String array using Arrays class copyOf() method.

a. First, just like above example convert the ArrayList to Object array using toArray() method.
b. Use Arrays.copyOf() method to convert Object array to String array.
c. Show the String array.


import java.util.*;

public class ConvertArrayListToArray3 {
    public static void main(String args[]) {
      // Declaring and initializing ArrayList in one step
      ArrayList<String> browsers = new ArrayList<>();
      browsers.add("Google Chrome");
      browsers.add("Mozilla Firefox");
      browsers.add("Edge");
      browsers.add("Opera");

      //Converting ArrayList to String array using copyOf()
      String[] browsernames = Arrays.copyOf(browsers.toArray(), browsers.size(), String[].class);

      // Displaying elements using for-each loop
      for(String str : browsernames) {
        System.out.println(str);      
      }
    }
}


Output:

Google Chrome
Mozilla Firefox
Edge
Opera

That's all for today. Please mention in the comments in case you have any questions related to how to convert ArrayList to String array in Java with examples.

About The Author

Subham Mittal has worked in Oracle for 3 years.
Enjoyed this post? Never miss out on future posts by subscribing JavaHungry