How do I convert LinkedList to array?

package org.kodejava.example.util;

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

public class LinkedListToArray {
    public static void main(String[] args) {
        List<String> list = new LinkedList<>();
        list.add("Blue");
        list.add("Green");
        list.add("Purple");
        list.add("Orange");

        // Converting LinkedList to array can be done by calling the toArray()
        // method of the List;
        String[] colors = new String[list.size()];
        list.toArray(colors);

        for (String color : colors) {
            System.out.println("color = " + color);
        }
    }
}

Wayan

Programmer, runner, recreational diver, live in the island of Bali, Indonesia. Mostly programming in Java, Spring Framework, Hibernate / JPA. Support me by donating.

Leave a Reply