zoukankan      html  css  js  c++  java
  • 集合元素去重复contains()方法使用

     1 package cn.arraylist.com;
     2 
     3 import java.util.ArrayList;
     4 import java.util.Iterator;
     5 /*
     6  * 集合去除相同元素原理:用集合和空集合对比,遍历集合>>如果空集合中没有有当前元素,则把当前元素添加到空集合中,
     7  * 最后遍历新集合
     8  */
     9 public class ArraylistDemo {
    10 
    11     public static void main(String[] args) {
    12         // TODO Auto-generated method stub
    13         ArrayList arraylist = new ArrayList();
    14         arraylist.add("li");
    15         arraylist.add("liu");
    16         arraylist.add("huang");
    17         arraylist.add("li");
    18         arraylist.add("huang");
    19         // 创建集合2
    20         ArrayList arraylist2 = new ArrayList();
    21 
    22         // 迭代器
    23         Iterator it = arraylist.iterator();
    24         while (it.hasNext()) {
    25             String s = (String) it.next();
    26             if (!arraylist2.contains(s)) {//如果新集合不包含当前元素
    27                 arraylist2.add(s);//添加当前元素到新集合
    28             }
    29         }
    30         // 遍历新集合
    31         Iterator it2 = arraylist2.iterator();
    32         while (it2.hasNext()) {
    33             String ss = (String) it2.next();
    34             System.out.println(ss);
    35         }
    36     }
    37 }
  • 相关阅读:
    Problem S: 分数类的模板数组类
    Problem E: 向量的运算
    Problem D: 强悍的矩阵运算来了
    Problem C: Person类与Student类的关系
    Problem B: 还会用继承吗?
    Problem A: 求个最大值
    Problem B: 数组类(II)
    树的直径题集
    LCA题集
    线段树总结
  • 原文地址:https://www.cnblogs.com/yschung/p/9316543.html
Copyright © 2011-2022 走看看