Java LinkedList toArray() Method
toArray() is a method in Java's LinkedList is a practical method provided by the class for converting all elements in the list into an array. This method belongs to java.util.LinkedList class, inherited from java.util.AbstractCollection class.
Method Syntax
LinkedList The class provides two toArray() overloaded forms of the method:
Basic form:
Object
[
]
toArray
(
)
Generic form:
<
T
T
[
]
toArray
(
T
[
]
a
)
Method details
Basic form toArray()
Example
Object
[
]
toArray
(
)
This method returns an array containing LinkedList all elements of Object array. The order of elements in the array is the same as the iteration order of elements in the list.
Feature:
- returns a
Object[]type array - The new array and the original
LinkedListare independent of each other - Modifying the returned array does not affect the original
LinkedList
Example Code:
Example
import
java.util.LinkedList
;
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
<
T
T
[
]
toArray
(
T
[
]
a
)
This method allows you to specify the type of the returned array, making it more flexible and safer than the basic form.
Parameters:
a: If the array is large enough, the elements are stored in this array; otherwise, a new array of the same runtime type is allocated
Return Value:
- Contains
LinkedListan array of all elements - If the specified array is large enough, that array is returned
- Otherwise, a newly allocated array is returned
Example Code:
Example
import
java.util.LinkedList
;
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 especially useful in the following situations:
- When you need to convert
LinkedListinto a fixed-size array - When some 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 list access)
Notes
- Type Safety: The basic form's
toArray()ReturnObject[], may require type casting - Array Size: For 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 - Performance considerations: Frequent conversion may affect performance, especially on large collections
- Concurrent Modification: If the collection is modified during conversion, it may throw
ConcurrentModificationException
Best Practices
- Use the generic form: Prefer to use
toArray(T[] a)to obtain a type-safe array - Specify the array size: You can use
list.size()to create an appropriately sized array - Empty array idiom:
toArray(new String[0])is a common and efficient usage
Recommended approach:
Example
LinkedList
<
String
list
=
new
LinkedList
<>
(
)
;
// Add elements...
// Best practice: use an empty array as the parameter
String
[
]
array
=
list.
toArray
(
new
String
[
0
]
)
;
// Or specify the size explicitly
String
[
]
sizedArray
=
list.
toArray
(
new
String
[
list.
size
(
)
]
)
;
Summary
LinkedList 's toArray() method provides a convenient way to convert a list into an array. Understanding the difference 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