Java LinkedList convert to array

Description

Java LinkedList convert to array

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

public class Main {
  // creates a LinkedList, adds elements and converts to array
  public static void main(String[] args) {
    String[] colors = { "black", "blue", "yellow" };
    LinkedList<String> links = new LinkedList<>(Arrays.asList(colors));


    // get LinkedList elements as an array
    colors = links.toArray(new String[links.size()]);

    for (String color : colors)
      System.out.println(color);//from   w  ww .ja va 2 s .  c o  m
  }
}



PreviousNext

Related