Java Code Example: convert array to LinkedList

Here we demonstrate how to convert an array to a LinkedList. First, a string array is converted to a list using the asList() function. This list is then converted into a LinkedList. The individual LinkedList elements are output by a for loop.

import java.util.Arrays;
import java.util.List;
import java.util.LinkedList;

class ArrayToLinkedListExample {
    public static void main(String[] args) {
        String[] daysString = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday"};
        
        List<String> daysList = Arrays.asList(daysString);
        LinkedList<String> daysLinkedList = new LinkedList<String>(daysList);
        
        for(String weekdays: daysLinkedList){
            System.out.println(weekdays);
        }
    }
}
Output
Monday
Tuesday
Wednesday
Thursday
Friday