- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
The toArray(T[]) method of AbstractSequentialList in Java
The difference between toArray() and toArray(T[] arr) is that both the methods returns an array containing all of the elements in this collection, but the latter has some additional features i.e. the runtime type of the returned array is that of the specified array.
The syntax is as follows:
public <T> T[] toArray(T[] arr)
Here, arr is the array into which the elements of this collection are to be stored,
To work with the AbstractSequentialList class in Java, you need to import the following package:
import java.util.AbstractSequentialList;
The following is an example to implement AbstractSequentialList toArray() method in Java:
Example
import java.util.LinkedList; import java.util.AbstractSequentialList; public class Demo { public static void main(String[] args) { AbstractSequentialList<Integer> absSequential = new LinkedList<>(); absSequential.add(210); absSequential.add(290); absSequential.add(350); absSequential.add(490); absSequential.add(540); absSequential.add(670); absSequential.add(870); System.out.println("Elements in the AbstractSequentialList = "+absSequential); Integer[] strArr = new Integer[5]; strArr = absSequential.toArray(strArr); System.out.println("Array = "); for (int i = 0; i < strArr.length; i++) System.out.println(strArr[i]); ; } }
Output
Elements in the AbstractSequentialList = [210, 290, 350, 490, 540, 670, 870] Array = 210 290 350 490 540 670 870
Let us see another example wherein we have the array size more than the total number of elements:
Example
import java.util.LinkedList; import java.util.AbstractSequentialList; public class Demo { public static void main(String[] args) { AbstractSequentialList<Integer> absSequential = new LinkedList<>(); absSequential.add(210); absSequential.add(290); absSequential.add(350); absSequential.add(490); System.out.println("Elements in the AbstractSequentialList = "+absSequential); Integer[] strArr = new Integer[7]; strArr = absSequential.toArray(strArr); System.out.println("Array = "); for (int i = 0; i < strArr.length; i++) System.out.println(strArr[i]); ; } }
Output
Elements in the AbstractSequentialList = [210, 290, 350, 490] Array = 210 290 350 490 null null null
Advertisements