Following methods can be used for converting ArrayList to Array:
Method 1: Using Object toArray() method
public Object toArray()
- It is specified by toArray in interface Collection
and interface List
- It overrides toArray in class AbstractCollection
- It returns an array containing all of the elements in this list in the correct order.
10 20 30 40
Note: toArray() method returns an array of type Object(Object). We need to typecast it to Integer before using as Integer objects. If we do not typecast, we get compilation error. Consider the following example:
19: error: incompatible types: Object cannot be converted to Integer Integer objects = al.toArray(); ^ 1 error
It is therefore recommended to create an array into which elements of List need to be stored and pass it as an argument in toArray() method to store elements if it is big enough. Otherwise a new array of the same type is allocated for this purpose.
Method 2: Using T toArray(T a)
// Converts a list into an array arr and returns same. // If arr is not big enough, then a new array of same // type is allocated for this purpose. // T represents generic. public
T toArray(T arr)
Note that the there is an array parameter and array return value. The main purpose of passed array is to tell the type of array. The returned array is of same type as passed array.
- If the passed array has enough space, then elements are stored in this array itself.
- If the passed array doesn’t have enough space, a new array is created with same type and size of given list.
- If the passed array has more space, the array is first filled with list elements, then null values are filled.
It throws ArrayStoreException if the runtime type of a is not a supertype of the runtime type of every element in this list.
10 20 30 40
Note : If the specified array is null then it will throw NullpointerException. See this for example.
Method 3: Manual method to convert ArrayList using get() method
We can use this method if we don’t want to use java in built toArray() method. This is a manual method of copying all the ArrayList
// Returns the element at the specified index in the list. public E get(int index)
10 20 30 40
This article is contributed by Nitsdheerendra. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Conversion of Array To ArrayList in Java
- Array to ArrayList Conversion in Java
- ArrayList toArray() method in Java with Examples
- ArrayList and LinkedList remove() methods in Java with Examples
- Array of ArrayList in Java
- Array vs ArrayList in Java
- Difference between length of Array and size of ArrayList in Java
- Buffer array() methods in Java with Examples
- Static methods vs Instance methods in Java
- Convert an ArrayList of String to a String array in Java
- ArrayList of ArrayList in Java
- Set toArray() method in Java with Example
- LongStream toArray() in Java
- DoubleStream toArray() in Java
- LinkedList toArray() method in Java with Example
- CopyOnWriteArraySet toArray() method in Java with Example
- ArrayDeque toArray() Method in Java
- LinkedBlockingDeque toArray() method in Java with Example
- AbstractSequentialList toArray(T) method in Java with Example
- AbstractSequentialList toArray() method in Java with Example
Improved By : nidhi_biet