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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 黄泉 中级黑马   /  2014-4-18 15:03  /  1197 人查看  /  4 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

今天复习集合!写了一个关于ArrayList去冲的代码!编译通过,就是出现了安全问题。
泛型该加的地方都加了,不知道哪里没加,请高手指点。
  1. import java.util.*;
  2. class Demo
  3. {
  4.         public static void main(String[] args)
  5.         {
  6.                 ArrayList<Student> al = new ArrayList<Student>();
  7.                 al.add(new Student("sad1",20));
  8.                 al.add(new Student("sad3",20));
  9.                 al.add(new Student("sad1",20));
  10.                 al.add(new Student("sad4",21));
  11.                
  12.                 al = quChong(al);
  13.                 for(Student s : al)//高级for循环,和迭代一一样的
  14.                 {
  15.                         System.out.println(s.getName()+"-"+s.getAge());
  16.                 }
  17.         }
  18.         public static ArrayList<Student> quChong(ArrayList<Student> al)
  19.         {
  20.                 ArrayList<Student> newAl = new ArrayList<Student>();

  21.                 for(Iterator<Student> it = al.iterator() ; it.hasNext();)
  22.                 {
  23.                         Student s = it.next();
  24.                         if(!(newAl.contains(s)))
  25.                         {
  26.                                 newAl.add(s);
  27.                         }
  28.                 }
  29.                 return newAl;
  30.         }
  31. }

  32. class Student
  33. {
  34.         private String name;
  35.         private int age;
  36.         Student(String name ,int age)
  37.         {
  38.                 this.name = name;
  39.                 this.age = age;
  40.         }

  41.         public String getName()
  42.         {
  43.                 return name;
  44.         }
  45.         public int getAge()
  46.         {
  47.                 return age;
  48.         }

  49.         public boolean equals(Object obj)
  50.         {
  51.                 if(!(obj instanceof Student))
  52.                         throw new RuntimeException("数据类型错误");

  53.                 Student s = (Student)obj;

  54.                 return s.name.equals(this.name) && s.age == this.age;
  55.         }
  56. }
复制代码


评分

参与人数 1黑马币 +1 收起 理由
枫儿 + 1 神马都是浮云

查看全部评分

4 个回复

倒序浏览
在我这   没有安全问题啊
回复 使用道具 举报
此代码没有安全隐患.
回复 使用道具 举报
我觉得你的安全隐患处在equals的重写上,
@Override
        public boolean equals(Object obj) {
                if (this == obj)
                        return true;
                if (obj == null)
                        return false;
                if (getClass() != obj.getClass())
                        return false;
                Student other = (Student) obj;
                if (age != other.age)
                        return false;
                if (name == null) {
                        if (other.name != null)
                                return false;
                } else if (!name.equals(other.name))
                        return false;
                return true;
        }
这样子写就可以减少比较方面的安全隐患.个人见解不一定完全正确
回复 使用道具 举报
public static ArrayList<Student> quChong(ArrayList<Student> al)
        {
                ArrayList<Student> newAl = new ArrayList<Student>();

                for(Iterator<Student> it = al.iterator() ; it.hasNext();)
                {
                        Student s = it.next();
                        if(!(newAl.contains(s)))
                        {
                                newAl.add(s);
                        }
                }
                return newAl;
        }
这个程序中的这条语句有问题   Student s = it.next();
it.next();返回的是一个Object类型,而你这传的是Student类型,所以类型需要将Object类型转换成Student类型
代码如下:Student s = (Student)it.next();
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马