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;
 }
 
}

上一篇:第三方框架FMDB


下一篇:多图片/文件上传 - SwfUpload/PlUpload