Python 官方文档:入门教程 => 点击学习
目录向List集合批量添加元素往集合中添加多个元素向List集合批量添加元素 List<String> list = new ArrayList<String>
List<String> list = new ArrayList<String>();
Collections.addAll(list, "a", "b", "c");
或
String [] array = new String[] {"a", "b", "c"};
List<String> list = new ArrayList<String>();
Collections.addAll(list, array);
或初始化时添加
List<String> list = new ArrayList<String>(){
{
this.add("a");
this.add("b");
this.add("c");
}
};
public class Demo01Collections {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
//往集合中添加多个元素
//public static <T> boolean addAll(Collection<T> c, T... elements):往集合中添加一些元素。
Collections.addAll(list,"a","b","c","d","e");
System.out.println(list);//[a, b, c, d, e]
//public static void shuffle(List<?> list) 打乱顺序:打乱集合顺序。
Collections.shuffle(list);
System.out.println(list);//[b, d, c, a, e], [b, d, c, a, e]
}
}
以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。
--结束END--
本文标题: Java向List集合中批量添加元素的实现方法
本文链接: https://www.lsjlt.com/news/148200.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0