我是靠谱客的博主 和谐大象,这篇文章主要介绍将数组转换为list集合,然后list中重复元素删除将list中所有元素分别用迭代器和增强for循环打印出来,现在分享给大家,希望可以做个参考。

复制代码
1
2
//案例 // .已知数组存放一批QQ号码,QQ号码最长为11位,

1先把数组转为集合。

复制代码
1
2
3
String[] strs ={"12345","67891","12347809933","98765432102","67891","12347809933"};         LinkedList<String>list=new LinkedList<>();         Collections.addAll(list,strs);

2 增强for去重后集合中的元素

复制代码
1
2
3
4
5
6
for (String msg : list) { if (!newlist.contains(msg)) { newlist.add(msg); } } System.out.println("增强for去重后集合中的元素:"+newlist);

3 迭代器去重后集合中的元素

复制代码
1
2
3
4
5
6
7
8
ListIterator<String>it=list.listIterator(); while (it.hasNext()){ String msg=it.next(); if (!newlist.contains(msg)){ newlist.add(msg); } } System.out.println("迭代器去重后集合中的元素:"+newlist);

 4 for去重后集合中的元素

复制代码
1
2
3
4
5
6
7
8
for (int i=0;i<list.size();i++){ String msg=list.get(i); if (!newlist.contains(msg)){ newlist.add(msg); } } System.out.println("for去重后集合中的元素:"+newlist); System.out.println("--------------------------------------");

最后

以上就是和谐大象最近收集整理的关于将数组转换为list集合,然后list中重复元素删除将list中所有元素分别用迭代器和增强for循环打印出来的全部内容,更多相关将数组转换为list集合内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(119)

评论列表共有 0 条评论

立即
投稿
返回
顶部