我们不能在 Trie 构造函数中初始化引用数组吗

Can we not initialize reference array in Trie constructor

this Trie 实现中,children 数组元素使用 for 循环分别分配 null 值。

TrieNode(){ 
    isEndOfWord = false; 
    for (int i = 0; i < ALPHABET_SIZE; i++) 
    children[i] = null; 
}

但是,默认情况下,当我们在 Java 中创建引用类型数组时,所有条目的默认值为 null,即:

TrieNode[] children = new TrieNode[ALPHABET_SIZE];

上述步骤将 children 数组条目的默认值分配为 null

是否需要在 TrieNode 构造函数内的 for 循环中再次进行 null 赋值?

不,不需要 - 对于每个 class 变量、实例变量或数组组件 Java 将始终分配合理的默认值(如 0 代表 int 或 null 代表 Object) - 你可以阅读更多 here

但是请注意,对于局部变量,它不是 guaranteed

The compiler will assign a reasonable default value for fields of the above types; for local variables, a default value is never assigned.

这就是您被迫手动初始化它的原因

public void f() {
    String s;
    System.out.println(s); // will cause Error: java: variable s might not have been initialized
}