返回一个空数组

2024-03-30

我试图想出返回空数组的最佳方法,而不是null.

有什么区别吗foo() and bar()?

private static File[] foo() {
    return Collections.emptyList().toArray(new File[0]);
}
private static File[] bar() {
    return new File[0];
}

返回空数组的另一种方法是使用常量,因为给定类型的所有空数组都是相同的。

private static final File[] NO_FILES = {};
private static File[] bar(){
    return NO_FILES;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

返回一个空数组 的相关文章

随机推荐