集册 Java实例教程 将数组作为列表查看并将列表转换为数组。

将数组作为列表查看并将列表转换为数组。

欢马劈雪     最近更新时间:2020-01-02 10:19:05

413
提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
将数组视为列表并将列表转换为数组。
/**from NowJava.com - 时代Java**/

import java.util.LinkedList;

import java.util.Arrays;


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));


      links.addLast("red"); // add as last item

      links.add("pink"); // add to the end
      /*
      N o w J a v a . c o m 提 供
      */

      links.add(3, "green"); // add at 3rd index

      links.addFirst("cyan"); // add as first item      


      // get LinkedList elements as an array     

      colors = links.toArray(
展开阅读全文