gpt4 book ai didi

java - "Safe"在 Collections.toArray() JavaDoc 中意味着什么?

转载 作者:搜寻专家 更新时间:2023-11-01 01:10:30 24 4
gpt4 key购买 nike

当我阅读java源代码时,我发现在集合接口(interface)中,会很安全

The returned array will be "safe" in that no references to it are Maintained by this collection.(In other words, this method must allocate a new array even if this collection is backed by an array).The caller is thus free to modify the returned array.

我不明白什么意思,你能给我举个例子吗?

最佳答案

当然:

List<String> list = Arrays.asList("foo", "bar", "baz");
String[] array = list.toArray(new String[0]);
array[0] = "qux";
System.out.println(list.get(0)); // still "foo"

关于java - "Safe"在 Collections.toArray() JavaDoc 中意味着什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18638847/

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