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 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 (int i = 0; i < colors.length; i++) {
            System.out.println("color = " + colors[i]);
        }
    }
}

Wayan Saryada

Programmer, runner, recreational diver, currently living in the island of Bali, Indonesia. Mostly programming in Java, creating web based application with Spring Framework, Hibernate / JPA.

Leave a Reply