Linked list to array in java

We can use toArray() method to convert linked list to array in java.

Syntax:

linkedList.toArray(strArray);

Example:

package com.w3spoint;
 
import java.util.LinkedList;
 
public class Test {
  public static void main(String args[]){
	LinkedList<String> linkedList = new LinkedList<String>();
	linkedList.add("Jai");
	linkedList.add("Mahesh");
	linkedList.add("Naren");
	linkedList.add("Vivek");
	linkedList.add("Vishal");
	linkedList.add("Hemant");
	System.out.println("Actual LinkedList:"+linkedList);
	String[] strArray = new String[linkedList.size()];
	linkedList.toArray(strArray);
        System.out.println("Array created:");
        for(String str:strArray){
            System.out.println(str);
        }
  }
}

Output

Actual LinkedList:[Jai, Mahesh, Naren, Vivek, Vishal, Hemant]
Array created:
Jai
Mahesh
Naren
Vivek
Vishal
Hemant
Please follow and like us:
Content Protection by DMCA.com
togel situs toto situs togel situs toto situs toto agen togel situs togel situs togel togel situs togel resmi situs togel situs togel situs toto link togel togel online