How to convert LinkedList to array in java

Assuming you have gone through LinkedList in java , Here we will see how to convert linked list to array in java . Linkedlist use dobly linkedlist to store data and array is index base data structure. We will use toArray() method for convert linked list to array in java , Let’s see all ways for  how to convert a linked list to array in java .

  1. public Object[] toArray();
  2. public <T> T[] toArray(T[] a);
  3. Public get() method;

LinkedList toArray() – convert to object array

toArray() Method returns an array containing all of the elements in this list in proper sequence
(from first to last element). The returned array will be “safe” in that no references to it are maintained by this list. (In other words, this method must allocate a new array). The caller is thus free to modify the returned array.

Syntax :

public Object[] toArray() {

}

It returns an array containing all of the elements in this list in proper sequence. Let’s see code for this

import java.util.LinkedList;

public class LinkedListToArray {
	
	public static void main(String[] args) {
		
		/*
		 * Constructs an empty list.
		 */
		LinkedList<String> linkedList = new LinkedList<String>();

		//adding element in linkedlist
		
		linkedList.add("PHP");
		linkedList.add("JAVA");
		linkedList.add("C++");
		linkedList.add("Android");
		
		//print linkedList
		System.out.println("Linkedlist is ="+ linkedList );
		
		/*
		 * Returns an array containing all of the elements in this list in 
		 * proper sequence (from first to last element). 
          The returned array will be "safe" in that no references to it are maintained
           by this list. (In other words, this method must allocate a new array). 
           The caller is thus free to modify the returned array. 
		 */
		 Object[] array = linkedList.toArray();
		 System.out.println("--->> print array elements ");
		//print array element
        for (Object object : array) {
		      System.out.println(object);	
		}
        
	}
}

Output :

Linkedlist is =[PHP, JAVA, C++, Android]
--->> print array elements 
PHP
JAVA
C++
Android

LinkedList toArray(T[] a) – convert to string array

Now we will convert linked list to array in java using toArray(T[] a) method returns an array containing all of the elements in this list in proper sequence (from first to last element); the runtime type of the returned array is that of the specified array.

  • Array have enough space then list element fill in that
  • If Array have not enough size then it will create new array with list size
  • if Array have more space then it fill by element of list first then fill by null values.

Syntax :

public <T> T[] toArray(T[] a) {

}

Parameters – a the array into which the elements of the list are to be stored.
Returns value – an array containing the elements of the list
Exception – it Throws following exceptions
ArrayStoreException – if the runtime type of the specified array is not a supertype of the runtime type of every element in this list
NullPointerException – if the specified array is null

import java.util.LinkedList;

public class LinkedListToArray2 {

	public static void main(String[] args) {
		/*
		 * Constructs an empty list.
		 */
		LinkedList<String> linkedList = new LinkedList<String>();

		// adding element in linkedlist

		linkedList.add("PHP");
		linkedList.add("JAVA");
		linkedList.add("C++");
		linkedList.add("Android");

		// print linkedList
		System.out.println("Linkedlist is =" + linkedList);

		String[] arr = new String[linkedList.size()];

		/*
		 Returns an array containing all of the elements in this list
		  in proper sequence (from first to last element); the runtime
		  type of the returned array is that of the specified array.
		   If the list fits in the specified array, it is returned therein.
		  Otherwise, a new array is allocated with the runtime type of 
		  the specified array and the size of this list. 
          If the list fits in the specified array with room to spare 
         (i.e., the array has more elements than the list),
        the element in the array immediately following the 
        end of the list is set to null.
        (This is useful in determining the length of 
        the list only if the caller 
        knows that the list does not contain any null elements.) 
		 */
		
		arr = linkedList.toArray(arr);
		System.out.println("===> print array ");
		// print array
		for (String str : arr) {
			System.out.println(str);
		}
	}

}

Output :

Linkedlist is =[PHP, JAVA, C++, Android]
===> print array 
PHP
JAVA
C++
Android

LinkedList get() – arraylist to string array java

Here will convert linked list to array in java using get() method , We iterate LinkedList and set element in array one by one .

import java.util.LinkedList;

public class LinkedListToArray3 {

	public static void main(String[] args) {
		/*
		 * Constructs an empty list.
		 */
		LinkedList<String> linkedList = new LinkedList<String>();

		// adding element in linkedlist

		linkedList.add("PHP");
		linkedList.add("JAVA");
		linkedList.add("C++");
		linkedList.add("Android");

		// print linkedList
		System.out.println("Linkedlist is =" + linkedList);

		String[] arr = new String[linkedList.size()];

		// iterate list and set element in array
		for (int i = 0; i < linkedList.size(); i++) {
			arr[i] = linkedList.get(i);
		}

		System.out.println("Element in Array");

		// print array
		for (String str : arr) {
			System.out.println(str);
		}

	}

}

Output :

Linkedlist is =[PHP, JAVA, C++, Android]
Element in Array
PHP
JAVA
C++
Android

A Guide To LinnkedList

Java docs – LinkedList

 

Leave a Reply

Your email address will not be published. Required fields are marked *

25 + = 27