Convert arraylist to array in Java
In this post, you will learn how to convert arraylist to array using the Java programming language.
ArrayList in Java
The ArrayList class implements the List interface. It uses a dynamic array to store the duplicate elements of different data types. ArrayList is a part of the collection framework and is present in java.util package. It is like an array, but there is no size limit. ArrayList is a dynamic array, and we do not have to specify the size while creating it. We can add or remove elements at anytime. ArrayList is not synchronized, which means multiple threads can work on ArrayList at the same time. It provides some useful methods like add(), addAll(), capacity(), clear(), clone(), elements(), equals(), size(), set(), remove(), and trimToSize().
ArrayList Declaration
ArrayList<Type> arrayList= new ArrayList<>();
Array in Java
An array is a data structure representing an ordered collection of indexed items. An array is used when there is a requirement to add more items in a single variable. In the development process, we may come to the situation where we need to convert arraylist to array. Java provides various approaches to achieve this task. Here, we have mentioned most of them.
Convert ArrayList to Array using Object[] toArray() method
Here, we have used the toArray() method from the Java List interface for converting an ArrayList to Array. It returns an array of type Object containing all of the elements in this list.
import java.io.*;
import java.util.List;
import java.util.ArrayList;
class ConvertToArray {
public static void main(String[] args)
{
List<Integer> arrList = new ArrayList<Integer>();
arrList.add(42);
arrList.add(82);
arrList.add(22);
arrList.add(72);
Object[] objects = arrList.toArray();
// Printing array of objects
for (Object obj : objects)
System.out.print(obj + " ");
}
}
Output of the above code:
42 82 22 72
Convert ArrayList to Array using toArray(T[] a)
In the given Java program, we have used the toArray(T[] a) method to convert an ArrayList to an Array. This method returns an array containing all of the elements in this list in proper sequence. If the returned array has enough space, then elements are stored in this array itself. If the array doesn't have enough space, a new array is allocated with the runtime type of the specified array and the size of this list. It the array has more space, it is first filled with list elements, then null values are filled.
// Java program to
// convert an ArrayList to arr[]
import java.io.*;
import java.util.List;
import java.util.ArrayList;
public class ConvertToArray {
public static void main(String[] args)
{
List<Integer> arrlist = new ArrayList<Integer>();
arrlist.add(89);
arrlist.add(14);
arrlist.add(74);
arrlist.add(52);
Integer[] arr = new Integer[arrlist.size()];
arr = arrlist.toArray(arr);
for (Integer x : arr)
System.out.print(x + " ");
}
}
Output of the above code:
89 14 74 52
Convert ArrayList to Array using get() method
The get() method of the List interface in Java is used to get the element present in this list at a given specific index.
Syntax-public E get(int index)
The parameter 'index' represents the index of the elements to return. It returns the element at the specified position in this list. Here, we have used this method in a loop to convert the ArrayList to Array.
// Java program to convert a ArrayList to an array
// using get() in a loop.
import java.io.*;
import java.util.List;
import java.util.ArrayList;
class ConvertToArray {
public static void main(String[] args)
{
List<Integer> arrList = new ArrayList<Integer>();
arrList.add(83);
arrList.add(92);
arrList.add(42);
arrList.add(52);
arrList.add(72);
Integer[] arr = new Integer[arrList.size()];
// ArrayList to Array Conversion
for (int i = 0; i < arrList.size(); i++)
arr[i] = arrList.get(i);
for (Integer x : arr)
System.out.print(x + " ");
}
}
Output of the above code:
83 92 42 52 72
Related Articles
Java string split multiple delimitersEnum with values in Java
Convert array to list in Java
Java random number between 1 and 100
Calculating percentage in Java
Multiplication table program in Java
Java dialogue box
Fibonacci series using recursion in Java
Java sum of array
Circular prime in Java
Vowel and Consonant program in Java
Convert binary to decimal in Java
Convert decimal to binary in Java
Convert decimal to octal in Java
Convert decimal to hexadecimal in Java
Simple interest program in Java
Check whether the given number is even or odd in java
Print prime numbers from 1 to 100 in Java
Java prime number program
Java program to convert celsius to fahrenheit
Java program to check leap year
Java program to find factorial of a number