Skip to main content

ArrayList to Array in Java

How to convert ArrayList to Array in Java

In this article, we are going to learn different alternatives to turn an ArrayList to Array in Java. If you don't know about the ArrayList in Java, read this article: All about ArrayList in Java.

Content of this article

How to convert ArrayList to Array in Java - Using toArray() method?

ArrayList provides a method named as toArray() used to turn an ArrayList to Array in Java. This method takes an argument of an empty array whose size must be greater-than or equal-to the size of ArrayList and put all the elements from arraylist to array in java. If the array size is smaller than the arraylist then a new array is created and returned with all the elements of the ArrayList.
Syntax of toArray()
T[] toArray(T[] arr);

ArrayList to Array Example 1: Using toArray() method

import java.util.ArrayList;
import java.util.Arrays;

public class ArrayListExample {
        public static void main(String[] args) {
                ArrayList<String> languages = new ArrayList<>(
                        Arrays.asList("Java", "C++", "Python", ".net", "PHP"));

                String[] languageArray = convertArrayListToArray(languages);
                for (String language : languageArray) {
                        System.out.println(language);
                }
        }

        private static String[] convertArrayListToArray(ArrayList<String> arrayList) {
                String[] array = new String[arrayList.size()];
                arrayList.toArray(array);
                return array;
        }
}
Output
Java
C++
Python
.net
PHP

How to convert ArrayList to Array in Java - Using for loop?

Steps to convert ArrayList to Array in Java - Using for loop:

  1. Create an array of size as same as size of ArrayList.
  2. Run traditional for-loop on ArrayList.
  3. Using get() method of ArrayList, fetch the element and put into the array by index.
  4. Return the array.

ArrayList to Array Example 2: Using for loop

import java.util.ArrayList;
import java.util.Arrays;

public class ArrayListExample {
        public static void main(String[] args) {
                ArrayList<String> languages = new ArrayList<>(
                        Arrays.asList("Java", "C++", "Python", ".net", "PHP"));

                String[] languageArray = convertArrayListToArray(languages);
                for (String language : languageArray) {
                        System.out.println(language);
                }
        }

        private static String[] convertArrayListToArray(ArrayList<String> arrayList) {
                String[] array = new String[arrayList.size()];
                for(int i=0 ; i < arrayList.size() ; i++) {
                        array[i] = arrayList.get(i);
                }
                return array;
        }
}
Output
Java
C++
Python
.net
PHP

How to convert ArrayList to Array in Java - Using Stream toArray() method?

If you are using Java 8, it will be very easy to convert ArrayList to Array in Java 8 using stream API and Stream API also provides toArray() method. Let see how we can do this:

ArrayList to Array Example 3: Using Stream toArray() method

import java.util.ArrayList;
import java.util.Arrays;

public class ArrayListExample {
        public static void main(String[] args) {
                ArrayList<String> languages = new ArrayList<>(
                        Arrays.asList("Java", "C++", "Python", ".net", "PHP"));

                String[] languageArray = convertArrayListToArray(languages);
                for (String language : languageArray) {
                        System.out.println(language);
                }
        }

        private static String[] convertArrayListToArray(ArrayList<String> arrayList) {
                return arrayList.stream().toArray(String[]::new);
        }
}
Output
Java
C++
Python
.net
PHP

Related Posts:


Conclusion

In this article, we have learned different alternatives to convert ArrayList to Array in Java using the toArray() method, using for loop. Also, we have learned how can to turn an ArrayList to Array in Java 8.

Comments