如何将此列表翻译成 Java?

How to translate this List to Java?

当我尝试翻译此 C# 代码时,我目前收到一个错误,要求提供某些标识符:

public static List<string> stopWords = new List<string> {"ON","OF","THE","AN","A" };

当前故障Java代码:

public static List<String> stopWords = new ArrayList<String {"ON","OF","THE","AN","A" };

你可以使用

List<String> stopWords = new ArrayList<>(Arrays.asList("ON","OF","THE","AN","A" ));

备选方案:

List<String> stopWords = new ArrayList<String>() {{
    add("ON");
    add("OF");
    add("THE");
    add("AN");
    add("A");
}};

将要放入列表中的单词添加到数组中,并使用 for 循环添加数组元素:

  List<String> stopWords = new ArrayList<String>();
  String[] words = new String[] {
          "ON","OF","THE","AN","A"
  };
  for(String s : words) {
      stopWords.add(s);
  }