LinkedList toArray() Method in Java
In Java, the toArray() method is used to convert a LinkedList into an Array. It returns the same LinkedList elements but in the form of an Array only.
Example: Here, we use toArray() to convert a LinkedList into an Array of Integer.
// Java Program to Demonstrate how to use toArray() method
// to convert a LinkedList into an array of Integer elements
import java.util.LinkedList;
class Geeks {
public static void main(String[] args)
{
// Creating an empty LinkedList of Integers
LinkedList<Integer> l = new LinkedList<Integer>();
// Use add() method to add elements
// into the LinkedList
l.add(10);
l.add(20);
l.add(30);
l.add(40);
l.add(50);
System.out.println("The LinkedList: " + l);
// Creating the array
Integer[] arr = new Integer[5];
// using toArray() to convert
// linkedlist into an array
l.toArray(arr);
// Printing all elements of the Array
System.out.print(
"After converting LinkedList to Array: ");
for (Integer element : arr) {
System.out.print(element + " ");
}
}
}
// Java Program to Demonstrate how to use toArray() method
// to convert a LinkedList into an array of Integer elements
import java.util.LinkedList;
class Geeks {
public static void main(String[] args)
{
// Creating an empty LinkedList of Integers
LinkedList<Integer> l = new LinkedList<Integer>();
// Use add() method to add elements
// into the LinkedList
l.add(10);
l.add(20);
l.add(30);
l.add(40);
l.add(50);
System.out.println("The LinkedList: " + l);
// Creating the array
Integer[] arr = new Integer[5];
// using toArray() to convert
// linkedlist into an array
l.toArray(arr);
// Printing all elements of the Array
System.out.print(
"After converting LinkedList to Array: ");
for (Integer element : arr) {
System.out.print(element + " ");
}
}
}
Output
The LinkedList: [10, 20, 30, 40, 50] After converting LinkedList to Array: 10 20 30 40 50
Now there are two method to convert LinkedList into an Array i.e. one with toArray() without parameter and one with toArray(arrayName) with parameter.
1. toArray() - Without Parameter
In Java, the toArray() method without parameter returns an array containing all the elements in the list in proper sequence . The returned array will be safe as a new array is created (hence new memory is allocated). Thus the caller is free to modify the array. It acts as a bridge between array-based and collection-based APIs.
Syntax:
Object[] toArray();
- Parameters: It does not take any parameter.
- Return Type: It returns an array of object containing all the elements in the list.
Example: Here, we use toArray() without parameter to convert a LinkedList into an Array of Integer.
// Java Program Demonstrate toArray()
// method of LinkedList
// without parameter (with Integer type LinkedList)
import java.util.LinkedList;
public class Geeks {
public static void main(String[] args)
{
// Create object of LinkedList
LinkedList<Integer> l = new LinkedList<Integer>();
// use add() to insert elements in a LinkedList
l.add(1);
l.add(2);
l.add(3);
l.add(4);
System.out.println("The LinkedList is: " + l);
// Convert LinkedList into an Array
// the method has no parameter
Object[] arr = l.toArray();
// Displaying all elements of the Array
System.out.print(
"After converted LinkedList to Array: ");
for (Object i : arr)
System.out.print(i + " ");
}
}
// Java Program Demonstrate toArray()
// method of LinkedList
// without parameter (with Integer type LinkedList)
import java.util.LinkedList;
public class Geeks {
public static void main(String[] args)
{
// Create object of LinkedList
LinkedList<Integer> l = new LinkedList<Integer>();
// use add() to insert elements in a LinkedList
l.add(1);
l.add(2);
l.add(3);
l.add(4);
System.out.println("The LinkedList is: " + l);
// Convert LinkedList into an Array
// the method has no parameter
Object[] arr = l.toArray();
// Displaying all elements of the Array
System.out.print(
"After converted LinkedList to Array: ");
for (Object i : arr)
System.out.print(i + " ");
}
}
Output
The LinkedList is: [1, 2, 3, 4] After converted LinkedList to Array: 1 2 3 4
2. toArray(arrayName) - With Parameter
In Java, the toArray() method with parameter also returns an array containing all the elements in the list in proper sequence.
- If the array is larger than the LinkedList, the toArray() method fills the array with the elements from the LinkedList, and the remaining positions are filled with null.
- If the array is smaller than the LinkedList, the toArray() method creates a new array of the correct size and fills the array with the elements from the LinkedList.
Syntax:
<T> T[] toArray(T[] a)
- Parameters: It takes an array as a parameter. which should be of the same type as the element of the LinkedList.
- Return Type: It return an array containing elements similar to the LinkedList.
Exceptions: The method might throw two types of exceptions
- ArrayStoreException: When the mentioned array is of a different type and is not able to compare with the elements mentioned in the LinkedList.
- NullPointerException: If the array is Null, then this exception is thrown.
Example: Here, we use toArray(arrayName) with parameter to convert a LinkedList into an Array of String.
// Java code to Demonstrate the working of toArray(arr[])
// with parameters to convert a LinkedList into a Array of String
import java.util.LinkedList;
public class Geeks{
public static void main(String args[])
{
// Creating an empty LinkedList
LinkedList<String> l = new LinkedList<String>();
// Use add() method to insert
// elements into the LinkedList
l.add("Welcome");
l.add("To");
l.add("Geeks");
l.add("For");
l.add("Geeks");
System.out.println("The LinkedList: " + l);
// Creating the array and using toArray()
String[] arr = new String[5];
l.toArray(arr);
// Displaying all elements of the Array
System.out.print(
"After converted LinkedList to Array: ");
for (String e : l)
System.out.print(e + " ");
}
}
// Java code to Demonstrate the working of toArray(arr[])
// with parameters to convert a LinkedList into a Array of String
import java.util.LinkedList;
public class Geeks{
public static void main(String args[])
{
// Creating an empty LinkedList
LinkedList<String> l = new LinkedList<String>();
// Use add() method to insert
// elements into the LinkedList
l.add("Welcome");
l.add("To");
l.add("Geeks");
l.add("For");
l.add("Geeks");
System.out.println("The LinkedList: " + l);
// Creating the array and using toArray()
String[] arr = new String[5];
l.toArray(arr);
// Displaying all elements of the Array
System.out.print(
"After converted LinkedList to Array: ");
for (String e : l)
System.out.print(e + " ");
}
}
Output
The LinkedList: [Welcome, To, Geeks, For, Geeks] After converted LinkedList to Array: Welcome To Geeks For Geeks