A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© xiaoya0o0o 中级黑马   /  2015-9-21 22:16  /  154 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文


  1. import java.util.ArrayList;
  2. import java.util.List;

  3. /*
  4. * 去除ArrayList中重复的元素:
  5. *
  6. * 以下两种方式都需要掌握:
  7. *
  8. * 方式一:定义一个新集合;
  9. * 方式二:遍历当前集合,在当前集合的基础上进行修改;
  10. */
  11. public class Demo {
  12.         public static void main(String[] args) {
  13.                 List list = new ArrayList();
  14.                 list.add(new Student("李四",22));
  15.                 list.add(new Student("李四",22));
  16.                 list.add(new Student("李四",22));
  17.                 list.add(new Student("张三",20));
  18.                 list.add(new Student("李四",22));
  19.                 list.add(new Student("李四",22));
  20.                 list.add(new Student("王五",24));
  21.                 list.add(new Student("王五",24));
  22.                 list.add(new Student("王五",24));
  23.                 list.add(new Student("王五",24));
  24.                 list.add(new Student("王五",24));
  25.                 /*
  26.                 //方式一:
  27.                 List newList = new ArrayList();
  28.                
  29.                 //遍历
  30.                 loop:
  31.                 for(int i = 0;i < list.size() ;i++){
  32.                         Student stu = (Student)list.get(i);
  33.                         for(int j = 0;j < newList.size() ; j++){
  34.                                 Student stu2 = (Student)newList.get(j);
  35.                                 if(stu.equals(stu2)){
  36.                                         continue loop;
  37.                                 }
  38.                         }
  39.                         //将stu添加到newList中
  40.                         newList.add(stu);
  41.                 }
  42.                
  43.                 //遍历newList
  44.                 for(int i = 0 ;i < newList.size() ; i++){
  45.                         Student stu = (Student)newList.get(i);
  46.                         System.out.println(stu.name + "," + stu.age);
  47.                 }*/
  48.                
  49.                 //方式二:
  50.                 for(int i = 0; i < list.size() - 1 ; i++){
  51.                         Student stu1 = (Student)list.get(i);
  52.                         for(int j = i + 1 ; j < list.size() ; j++){
  53.                                 Student stu2 = (Student)list.get(j);
  54.                                 if(stu1.equals(stu2)){
  55.                                         list.remove(j);
  56.                                         j--;
  57.                                 }
  58.                         }
  59.                 }
  60.                 //遍历原集合
  61.                 for(int i = 0; i < list.size() ; i++){
  62.                         Student stu = (Student)list.get(i);
  63.                         System.out.println(stu.name + "," + stu.age);
  64.                 }
  65.                
  66.         }
  67. }
复制代码

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马