The toArray(arr[]) method of AbstractSequentialList class in Java is used to form an array of the same elements as that of the AbstractSequentialList. It returns an array containing all of the elements in this AbstractSequentialList in the correct order; the run-time type of the returned array is that of the specified array. If the AbstractSequentialList fits in the specified array, it is returned therein. Otherwise, a new array is allocated with the run time type of the specified array and the size of this AbstractSequentialList. If the AbstractSequentialList fits in the specified array with room to spare (i.e., the array has more elements than the AbstractSequentialList), the element in the array immediately following the end of the AbstractSequentialList is set to null. (This is useful in determining the length of the AbstractSequentialList only if the caller knows that the AbstractSequentialList does not contain any null elements.) Syntax:
public <T> T[] toArray(T[] a)
Parameters: The method accepts one parameter arr[] which is the array into which the elements of the AbstractSequentialList are to be stored, if it is big enough; otherwise, a new array of the same runtime type is allocated for this purpose. Return Value: The method returns an array containing the elements similar to the AbstractSequentialList. Exception: The method might throw two types of exception:
- ArrayStoreException: When the mentioned array is of the different type and is not able to compare with the elements mentioned in the AbstractSequentialList.
- NullPointerException: If the array is Null, then this exception is thrown.
Below program illustrates the working of the AbstractSequentialList.toArray(arr[]) method. Program 1: When array is of the size of AbstractSequentialList
Java
import java.util.*;
public class AbstractSequentialListDemo {
public static void main(String args[])
{
AbstractSequentialList<String>
abs_col = new LinkedList<String>();
abs_col.add("Welcome");
abs_col.add("To");
abs_col.add("Geeks");
abs_col.add("For");
abs_col.add("Geeks");
System.out.println("The AbstractSequentialList: "
+ abs_col);
String[] arr = new String[ 5 ];
arr = abs_col.toArray(arr);
System.out.println("The arr[] is:");
for ( int j = 0 ; j < arr.length; j++)
System.out.println(arr[j]);
}
}
|
Output:
The AbstractSequentialList: [Welcome, To, Geeks, For, Geeks]
The arr[] is:
Welcome
To
Geeks
For
Geeks
Program 2: When array is less than the size of AbstractSequentialList
Java
import java.util.*;
public class AbstractSequentialListDemo {
public static void main(String args[])
{
AbstractSequentialList<String>
abs_col = new LinkedList<String>();
abs_col.add("Welcome");
abs_col.add("To");
abs_col.add("Geeks");
abs_col.add("For");
abs_col.add("Geeks");
System.out.println("The AbstractSequentialList: "
+ abs_col);
String[] arr = new String[ 1 ];
arr = abs_col.toArray(arr);
System.out.println("The arr[] is:");
for ( int j = 0 ; j < arr.length; j++)
System.out.println(arr[j]);
}
}
|
Output:
The AbstractSequentialList: [Welcome, To, Geeks, For, Geeks]
The arr[] is:
Welcome
To
Geeks
For
Geeks
Program 3: When array is more than the size of AbstractSequentialList
Java
import java.util.*;
public class AbstractSequentialListDemo {
public static void main(String args[])
{
AbstractSequentialList<String>
abs_col = new LinkedList<String>();
abs_col.add("Welcome");
abs_col.add("To");
abs_col.add("Geeks");
abs_col.add("For");
abs_col.add("Geeks");
System.out.println("The AbstractSequentialList: "
+ abs_col);
String[] arr = new String[ 10 ];
arr = abs_col.toArray(arr);
System.out.println("The arr[] is:");
for ( int j = 0 ; j < arr.length; j++)
System.out.println(arr[j]);
}
}
|
Output:
The AbstractSequentialList: [Welcome, To, Geeks, For, Geeks]
The arr[] is:
Welcome
To
Geeks
For
Geeks
null
null
null
null
null
Program 4: To demonstrate NullPointerException
Java
import java.util.*;
public class AbstractSequentialListDemo {
public static void main(String args[])
{
AbstractSequentialList<String>
abs_col = new LinkedList<String>();
abs_col.add("Welcome");
abs_col.add("To");
abs_col.add("Geeks");
abs_col.add("For");
abs_col.add("Geeks");
System.out.println("The AbstractSequentialList: "
+ abs_col);
try {
String[] arr = null ;
arr = abs_col.toArray(arr);
System.out.println("The arr[] is:");
for ( int j = 0 ; j < arr.length; j++)
System.out.println(arr[j]);
}
catch (Exception e) {
System.out.println("Exception: " + e);
}
}
}
|
Output:
The AbstractSequentialList: [Welcome, To, Geeks, For, Geeks]
Exception: java.lang.NullPointerException
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
10 Aug, 2022
Like Article
Save Article