【发布时间】:2018-03-30 15:13:16
【问题描述】:
我有一些这样的代码:
String[] colors = {"black", "blue", "yellow", "red", "pink", "green", "cyan"};
String[] alphabets = {"A", "B", "C", "D", "E", "F", "G", "H", "I"};
LinkedList<String> links = new LinkedList<>(Arrays.asList(colors));
System.out.println(links);
colors = links.toArray(alphabets);
System.out.println(Arrays.asList(colors));
它的输出是:
[black, blue, yellow, red, pink, green, cyan]
[black, blue, yellow, red, pink, green, cyan, null, I]
第二个输出行中的 null 是什么?为什么?
【问题讨论】:
-
RTM: 如果列表适合指定的数组并有剩余空间(即数组的元素多于列表),则数组中紧跟列表设置为空。