Convert an Array to LinkedList in Java


Write a Java Program to convert an array to linkedlist.

Click Here to View the Solution!
import java.util.Arrays;
import java.util.LinkedList;

public class ArrayToLinkedList {
    public static void main(String[] args) {

        // create an array
        String[] array = {"Java", "Python", "C","Javascript", "CPP"  };
        System.out.println("Array: " + Arrays.toString(array));

        // convert array to a linked list
        LinkedList<String> languages= new LinkedList<>(Arrays.asList(array));
        System.out.println("LinkedList: " + languages);
Click Here to View the Output!
Array: [Java, Python, C, Javascript, CPP]
LinkedList: [Java, Python, C, Javascript, CPP]
Click Here to View the Explanation!
  • In this program, first a String type array is initialized.
  • This array is then printed to the screen using toString() method.
  • The array is converted into a LinkedList called languages using the asList() method of the Array class, where array is passed as a parameter.
  • The new LinkedList is then printed onto the screen.

%d bloggers like this: