提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
将数组视为列表并将列表转换为数组。
/**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(