zoukankan      html  css  js  c++  java
  • Android 两个ArrayList找出相同元素及单个ArrayList删除元素

    //从一个ArrayList中删除重复元素

    List<String> arrayList1 = new ArrayList<String>();

    arrayList1.add("C");

    arrayList1.add("A");

    arrayList1.add("B");

    arrayList1.add("A");

    arrayList1.add("B");

    arrayList1.add("C");

    HashSet<String> hashSet = new HashSet<String>(arrayList1);

    List<String> arrayList2 = new ArrayList<String>(hashSet);

    for (Object item : arrayList2)

    System.out.println(item);

    //比较两个list中相同的值

    //假设比较的List分别为:A,B 建立中间变量C。

    首先将A的值克隆给C。使用C.removeAll(B)的方法。这样C中存在的既是A和B中不同的内容。

    最后在使用A.removeAll(C)这样最后A中留下的内容即为A,B中相同的内容。

    ArrayList<String> arrayList1 = new ArrayList<String>();

    arrayList1.add("R");

    arrayList1.add("A");

    arrayList1.add("B");

    arrayList1.add("c");

    arrayList1.add("B");

    arrayList1.add("C");

    //注意:List并没有clone()方法

    ArrayList<String> arrayList2 = new ArrayList<String>();

    arrayList2.add("W");

    arrayList2.add("HR");

    arrayList2.add("Y");

    arrayList2.add("C");

    arrayList2.add("A");

    arrayList2.add("B");

    //如果下一句改为:

    ArrayList<String> c = arrayList1;

    则后面的arrayList1同c指向同一对象,即,c改变arrayList1也跟着改变。

    ArrayList<String> c = (ArrayList<String>) arrayList1.clone();

    System.out.println("c " +c);

    c.removeAll(arrayList2);

    System.out.println("asdjfaawwq" +c);

    System.out.println("A "+arrayList1 );

    arrayList1.removeAll(c);

    System.out.println("array" +arrayList1);

  • 相关阅读:
    Java Map遍历方式的选择
    UriMatcher类的addURI()方法
    Java IO流分析整理[转]
    java基础一些注意细节
    java中static变量和方存在内存什么区域
    详细解析Java中抽象类和接口的区别
    mybatis一些记录
    Go语言简介(上)— 语法
    JavaScript相关-深入面向对象
    33个组件5
  • 原文地址:https://www.cnblogs.com/Cherry-B/p/3338184.html
Copyright © 2011-2022 走看看