Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Java LinkedList toArray() Method

Java LinkedList toArray() Method

Java LinkedList Java LinkedList


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:

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:

Return Value:

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:

  1. When you need to convert LinkedList into a fixed-size array
  2. When some APIs or methods only accept arrays as parameters
  3. When you need to perform array-specific operations on collection elements
  4. When you need to improve the performance of certain operations (array access is usually faster than list access)

Notes

  1. Type Safety: The basic form's toArray() Return Object[], may require type casting
  2. 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
  3. Performance considerations: Frequent conversion may affect performance, especially on large collections
  4. Concurrent Modification: If the collection is modified during conversion, it may throw ConcurrentModificationException

Best Practices

  1. Use the generic form: Prefer to use toArray(T[] a) to obtain a type-safe array
  2. Specify the array size: You can use list.size() to create an appropriately sized array
  3. 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 Java LinkedList