zoukankan      html  css  js  c++  java
  • List集合的去除重复性练习

    package com.java.b.listdmeo.www;

    import java.util.ArrayList;
    import java.util.Iterator;

    import com.java.Student.www.Student;

    public class listtest {

     public static void main(String[] args) {
      //创建一个集合,该集合为List 集合
      ArrayList list=new ArrayList();
      list.add(new Student("小火",28));
      list.add(new Student("小火",28));
      list.add(new Student("小惠",29));
      list.add(new Student("小惠",29));
      /*  
      list.add("babu1");
      list.add("babu2");
      list.add("babu1");
      list.add("babu4");
      list.add("babu4");

      for (Iterator it = list.iterator(); it.hasNext();) {
       Object object = (Object) it.next();
       System.out.print(object+" ");
      }*/

      //创建一个临时容器
      ArrayList list1=new ArrayList();
      //把元素一个一个放入临时容器中,中间再判断是否重复
      for (Iterator it = list.iterator(); it.hasNext();) {
       if(!list1.contains(it.next())){
        list1.add(it.next());
       } 
      }
      //清空原容器
      list.clear();
      //把临时容器的元素加入到原容器中
      list.addAll(list1);
      //遍历一下集合
      for (Iterator it = list.iterator(); it.hasNext();) {
       Object object = (Object) it.next(); 
       System.out.print(object+" ");
      }
     }
    }

    package com.java.Student.www;

    public class Student {
     private String name;
     private int age;
     
     public Student() {
      super();
     }
     
     public Student(String name, int age) {
      super();
      this.name = name;
      this.age = age;
     }

     public String getName() {
      return name;
     }
     public void setName(String name) {
      this.name = name;
     }
     public int getAge() {
      return age;
     }
     public void setAge(int age) {
      this.age = age;
     }
     @Override
     public String toString() {
      return "Student [name=" + name + ", age=" + age + "]";
     }
     public boolean pan(Object obj){
      if(this==obj){
       return true;
      }
      if(!(obj instanceof Student)){
       throw new ClassCastException();
      }
      Student stu=(Student)obj;
      return this.name .equals(stu.name)&&this.age ==stu.age;
     }
     
    }

  • 相关阅读:
    字体下载大宝库:30款好看的免费英文字体
    jQuery Mapael – 呈现动态的矢量地图
    Qt:用 __thread 关键字让每个线程有自己的全局变量
    从C++到Qt(舍弃IDE或qmake、cmake等工具的束缚,尝试通过几个例子)
    C++11(及现代C++风格)和快速迭代式开发
    EventBus + Redis发布订阅模式
    并发、并行和高并发
    Span<T>和Memory<T>
    Lucene.Net做一个简单的搜索引擎-全文索引
    技术架构演变
  • 原文地址:https://www.cnblogs.com/daoxiang1992/p/5730492.html
Copyright © 2011-2022 走看看