我正在尝试将字符串列表初始化为私有静态最终值,但我总是将值l设为 null,如果我按照第二个代码片段所示声明它,它就可以工作。
我正在尝试做的是将我声明的列表的元素添加到 trie 中,以便稍后匹配某些模式......
关于为什么第一个示例中的空值以及如何更正它的任何想法?谢谢
public class Myclass {
public static final Myclass INSTANCE = new Myclass();
private static final List<String> l = Arrays.asList("ofo", "oof", "foo");
private Trie trie;
private Myclass() {
trie = buildTrie();
}
private Trie buildTrie() {
TrieBuilder builder = Trie.builder();
Iterator<String> iterator = l.iterator();
while (iterator.hasNext()) {
builder.addKeyword(iterator.next());
}
return builder.build();
}
}
public class Myclass {
public static final Myclass INSTANCE = new Myclass();
private Trie trie;
private Myclass() {
List<String> l = Arrays.asList("ofo", "oof", "foo");
trie = buildTrie();
}
private Trie buildTrie() {
TrieBuilder builder = Trie.builder();
Iterator<String> iterator = l.iterator();
while (iterator.hasNext()) {
builder.addKeyword(iterator.next());
}
return builder.build();
}
}
繁星淼淼
相关分类