如何将此List转换为Java?

时间:2015-08-20 16:15:31

标签: java c# list arraylist code-translation

我正在收到错误,要求在尝试翻译此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" };

3 个答案:

答案 0 :(得分:4)

您可以使用

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

答案 1 :(得分:2)

替代方案:

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

答案 2 :(得分:0)

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

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