gpt4 book ai didi

java - 可修改列表的接口(interface)

转载 作者:行者123 更新时间:2023-12-02 05:32:03 24 4
gpt4 key购买 nike

所以我有一个界面:

public interface
{
List<String> getList();
}

但是,我相信实现者可以返回一个不可修改的列表,例如:

List<String> getList()
{
return Collections.unmodifiablelist(new ArrayList<String>());
}

是否有任何接口(interface)或方法来确保始终返回可修改的列表。我不想复印。

最佳答案

你不能保证,因为返回类型是Collection。你能做的是一个正确的文档。

In terms of API design, you should just make your javadoc clear ! People who use a method without reading its doc deserve the surprise.

Is it acceptable to return unmodifiableList or should I return array?

关于java - 可修改列表的接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17701601/

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