List<String> nameList=new ArrayList<>();
String str=sc.nextLine();
①while(true)
{
if(!nameList.contains(str)) //去重,耗时长
{
nameList.add(str);
}
str=sc.nextLine();}
② arrayList = new ArrayList<String>(new HashSet<String>(arrayList));
ArrayList直接删除重复元素
最新推荐文章于 2020-08-07 08:59:18 发布
本文探讨了在Java中使用ArrayList进行去重的两种方法:一种是通过while循环检查元素是否已存在于列表中;另一种是利用HashSet的特性快速去除重复项。后者效率更高,避免了多次遍历。
1038

被折叠的 条评论
为什么被折叠?



