gpt4 book ai didi

java - 将包含数据的数组传递给 List 的 toArray 方法

转载 作者:行者123 更新时间:2023-12-01 07:01:11 24 4
gpt4 key购买 nike

我有一些这样的代码:

    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 是什么?为什么?

最佳答案

当您对某个方法的特定行为有疑问时,最好的答案来源是查看文档。

Java 文档指出:

toArray(T[] a)

If the list fits in the specified array with room to spare (i.e., the array has more elements than the list), the element in the array immediately following the end of the list is set to null. (This is useful in determining the length of the list only if the caller knows that the list does not contain any null elements.)

关于java - 将包含数据的数组传递给 List 的 toArray 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49576881/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com