Java Code Example: convert array to LinkedList

The program converts an array of strings into a linked list and then prints the elements of the linked list. The array of strings daysString is converted to a list daysList using the Arrays.asList() method. The list is then used to initialize a linked list daysLinkedList using the constructor LinkedList(List<String> list). Finally, the program uses a for-each loop to print the elements of the linked list.

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