Java LinkedList toArray() Method
toArray()is a practical method in JavaLinkedListprovided by the class, used to convert all elements in a linked list into an array. This method belongs to thejava.util.LinkedListclass and is inherited from thejava.util.AbstractCollectionclass.
Method Syntax
LinkedListThe class provides twotoArray()overloaded forms of the method:
Basic Form:
Generic Form:
Method Details
Basic Form toArray()
Example
This method returns an array containingLinkedListall elements in theObjectarray. The order of elements in the array is the same as the iteration order of elements in the linked list.
Characteristics:
- Returns an
Object[]type array - The new array is independent of the original
LinkedListlinked list - Modifying the returned array does not affect the original
LinkedList
linked list:
Example
public class Main {
public static void main(String[] args) {
LinkedList<String> list = new LinkedList<>();
list.add("Apple");
list.add("Banana");
list.add("Cherry");
Object[] array = list.toArray();
for (Object item : array) {
System.out.println(item);
}
}
}
Generic Form toArray(T[] a)
Example
Example
This method allows you to specify the type of the returned array, making it more flexible and safer than the basic form.:
aParameter
Returns an array containing:
- all elements of the
LinkedListlinked list - If the specified array is large enough, that array is returned
- Otherwise, a newly allocated array is returned
Example Code:
Example
public class Main {
public static void main(String[] args) {
LinkedList<String> list = new LinkedList<>();
list.add("Apple");
list.add("Banana");
list.add("Cherry");
String[] array = list.toArray(new String[0]);
for (String item : array) {
System.out.println(item);
}
}
}
Use Cases
toArray()The method is particularly useful in the following situations:
- When you need to convert a
LinkedListlinked list into a fixed-size array - When certain APIs or methods only accept arrays as parameters
- When you need to perform array-specific operations on collection elements
- When you need to improve the performance of certain operations (array access is usually faster than linked list access)
Notes
- Type safetyThe basic form of
toArray()toArray() returnsObject[]Object[], which may require type casting - Array sizeFor the generic form, if the passed array is too small, a new array is automatically created; if the passed array is too large, the extra positions are set to
null - nullPerformance considerations
- Frequent conversion may affect performance, especially on large collectionsConcurrent modification
ConcurrentModificationException
Best Practices
- ConcurrentModificationException may be thrownBest Practices
toArray(T[] a)Use the generic form - Prefer usingtoArray(T[] a) to obtain type-safe arrays
list.size()Specify array size - You can use:
toArray(new String[0])new String
Recommended approach:
Example
// Add elements...
// Best practice: use an empty array as the parameter
String[] array = list.toArray(new String[0]);
// Or explicitly specify the size
String[] sizedArray = list.toArray(new String[list.size()]);
Summary
LinkedListoftoArray()The method provides a convenient way to convert a linked list into an array. Understanding the differences and applicable scenarios of these two forms (basic form and generic form) can help you write more efficient and safer code. In actual development, it is recommended to use the generic form for better type safety.
Java LinkedList