Java 向 List<String> 添加值 = new ArrayList<String>();
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22282820/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 14:43:52 来源:igfitidea点击:
Adding value to List<String> = new ArrayList<String>();
提问by lonelearner
Sorry i really dont know how to do this, last resort but to ask. I wanted to add values to the string list. But my code is not working
对不起,我真的不知道该怎么做,最后只能问。我想将值添加到字符串列表中。但我的代码不起作用
private List<String> sample = new ArrayList<String>(){"item1","item2","item3"};
采纳答案by Rakesh KR
Try,
尝试,
ArrayList<String> list = new ArrayList<String>() {{
add("item1");
add("item2");
add("item3");
}}
OR
或者
ArrayList<String> list = new ArrayList<String>();
list.add("item1");
list.add("item2");
list.add("item3");
回答by wypieprz
Here it is:
这里是:
List<String> sample = new ArrayList<String>(Arrays.asList("item1", "item2", "item3"));