这个问题涉及函数 public List inorderTraversal () .
假设 root 为 null,返回的 List
为空,即 size = 0。我现在很困惑,因为这与 Collections.emptyList() 相同。我们什么时候应该使用 Collections.emptyList ?这个 inorderTraversal 是否适合使用它而不是大小为 0 的列表?
最佳答案
public static void main(String[] args) {
List l = Collections.emptyList();
l.clear(); // No NPE
List p = null;
p.clear(); // NPE
}
因此,更愿意返回空列表而不是返回 null..
关于java - 何时返回空集合,何时不返回?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21156921/