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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© zhkqy 中级黑马   /  2013-12-10 20:36  /  1195 人查看  /  3 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

有这样一个数组如下

序号  姓名    年龄    身高
01    张三    10      170
02    李四    20      175
03    马五    20      175
04    王六    20      175
05    孙七    23      180
06    陈八    23      180
07    赵九    30      160
08    吴十    30      160
....

如何只通过年龄与身高去除重复的元素。
而姓名不管,只保留第一次出现的名字
去除重复后变成如下数组

序号  姓名    年龄    身高
01    张三    10      170
02    李四    20      175
03    孙七    23      180
04    赵九    30      160
....
困扰了好久,最好能给出代码例子。
用数组或list做出来都可以。谢谢了!

3 个回复

倒序浏览
  1. import java.util.*;

  2. class  ListDemo
  3. {
  4.         public static void main(String[] args)
  5.         {
  6.                 ArrayList al = new ArrayList();
  7.                
  8.                 al.add(new Person("张三",10,170));
  9.                 al.add(new Person("李四",20,150));
  10.                 al.add(new Person("王五",20,150));
  11.                 al.add(new Person("小六",22,184));
  12.                 al.add(new Person("老二",24,178));
  13.                 al.add(new Person("老四",23,182));
  14.                 al.add(new Person("本帅",23,190));
  15.                 al.add(new Person("赵启",23,182));

  16.                 al = singleElement(al);
  17.                 Iterator it = al.iterator();

  18.                 while(it.hasNext())
  19.                 {
  20.                         Person p = (Person)it.next();
  21.                         sop(p.getName()+".."+p.getAge()+":"+p.getHigh());
  22.                 }
  23.         }
  24.         public static void sop(Object obj)
  25.         {
  26.                 System.out.println(obj);
  27.         }

  28.        
  29.         public static ArrayList singleElement(ArrayList al)
  30.         {
  31.                 //定义一个临时容器。
  32.                 ArrayList newAl = new ArrayList();

  33.                 Iterator it = al.iterator();

  34.                 while(it.hasNext())
  35.                 {
  36.                         Object obj = it.next();

  37.                         if(!newAl.contains(obj))//判断newAl中是否包含该元素,没有的话就加上。
  38.                                 newAl.add(obj);
  39.                 }
  40.                 return newAl;
  41.         }
  42.        
  43. }
  44. class Person
  45. {
  46.         private String name;
  47.         private int age;
  48.         private int high;
  49.         Person(String name,int age,int high)
  50.         {
  51.                 this.name = name;
  52.                 this.age = age;
  53.                 this.high = high;
  54.         }
  55.         public boolean equals(Object obj)
  56.         {
  57.                 if(!(obj instanceof Person))
  58.                         return false;
  59.                 Person p = (Person)obj;
  60.                 return this.age==p.age&&this.high==p.high;
  61.         }
  62.         public String getName()
  63.         {
  64.                 return name;
  65.         }
  66.         public int getAge()
  67.         {
  68.                 return age;
  69.         }
  70.         public int getHigh()
  71.         {
  72.                 return high;
  73.         }

  74. }
复制代码

除了人名没有改以外,其他要求都实现了。
建议用Collection.fill()去该名字~自己想去吧

评分

参与人数 1技术分 +1 收起 理由
简★零度 + 1

查看全部评分

回复 使用道具 举报
  1. public class Demo2 {

  2.        
  3.         public static void main(String[] args) {
  4.                 Student s1 = new Student("01", "张三", 10, 170);
  5.                 Student s2 = new Student("02", "李四", 20, 175);
  6.                 Student s3 = new Student("03", "马五", 20, 175);
  7.                 Student s4 = new Student("04", "王六", 20, 175);
  8.                 Student s5 = new Student("05", "孙七", 23, 180);
  9.                 Student s6 = new Student("06", "陈八", 23, 180);
  10.                 Student s7 = new Student("07", "赵九", 30, 160);
  11.                 Student s8 = new Student("08", "吴十", 30, 160);
  12.                 Student[] stus1 = {s1,s2,s3,s4,s5,s6,s7,s8};
  13.                 for(int i = 0 ; i < stus1.length;i++){
  14.                         for(int j = 0 ; j < stus1.length;j++){
  15.                                 if(i != j && stus1[i]!= null &&stus1[j] != null && stus1[i].equals(stus1[j])){
  16.                                         stus1[j]=null;
  17.                                 }
  18.                         }
  19.                 }
  20.                 for(int i = 0 ; i < stus1.length;i++){
  21.                         System.out.println(stus1[i] == null ? "" : stus1[i].toString());
  22.                 }
  23.         }

  24. }
  25. class Student{
  26.         private String no;
  27.         private String name;
  28.         private int age;
  29.         private int height;
  30.        
  31.         public Student(String no, String name, int age, int height) {
  32.                 super();
  33.                 this.no = no;
  34.                 this.name = name;
  35.                 this.age = age;
  36.                 this.height = height;
  37.         }
  38.         public String getNo() {
  39.                 return no;
  40.         }
  41.         public void setNo(String no) {
  42.                 this.no = no;
  43.         }
  44.         public String getName() {
  45.                 return name;
  46.         }
  47.         public void setName(String name) {
  48.                 this.name = name;
  49.         }
  50.         public int getAge() {
  51.                 return age;
  52.         }
  53.         public void setAge(int age) {
  54.                 this.age = age;
  55.         }
  56.         public int getHeight() {
  57.                 return height;
  58.         }
  59.         public void setHeight(int height) {
  60.                 this.height = height;
  61.         }

  62.         public boolean equals(Student stu) {
  63.                 return (this.age+""+this.height).equals(stu.getAge()+""+stu.getHeight());
  64.         }
  65.         @Override
  66.         public String toString() {
  67.                 return "Student [no=" + no + ", name=" + name + ", age=" + age
  68.                                 + ", height=" + height + "]";
  69.         }
  70.        
  71. }
复制代码

实现的方法用的最简单无脑的,剩下的自己思考吧,如果看不懂找我给你加注释哈

评分

参与人数 1技术分 +1 收起 理由
简★零度 + 1

查看全部评分

回复 使用道具 举报
  1. public class Test2
  2. {
  3.     public static void main(String[] args)
  4.     {
  5.             List<Person2> list=new ArrayList<Person2>();
  6.             List<Person2> list2 =new ArrayList<Person2>();
  7.             list.add(new Person2("01","one7",17,170));
  8.             list.add(new Person2("01","one5",17,170));
  9.             list.add(new Person2("01","one6",17,170));
  10.             list.add(new Person2("02","one2",17,170));
  11.             list.add(new Person2("03","one3",27,180));
  12.             list.add(new Person2("04","one4",37,160));
  13.             Iterator<Person2> it=list.iterator();
  14.             Iterator<Person2> it2=list2.iterator();
  15.             while(it.hasNext())
  16.             {
  17.                     Person2 p=(Person2)it.next();
  18.                     if(!list2.contains(p))
  19.                     {
  20.                             list2.add(p);
  21.                     }
  22.             }
  23.            
  24.             System.out.println(list2);
  25.     }
  26. }
  27. class Person2 {
  28.         private String num;
  29.         private String name;
  30.         private int age;
  31.         private int height;

  32.         Person2(String num, String name, int age, int height) {
  33.                 this.num = num;
  34.                 this.name = name;
  35.                 this.age = age;
  36.                 this.height = height;
  37.         }

  38.         public String getNum() {
  39.                 return num;
  40.         }

  41.         public void setNum(String num) {
  42.                 this.num = num;
  43.         }

  44.         public String getName() {
  45.                 return name;
  46.         }

  47.         public void setName(String name) {
  48.                 this.name = name;
  49.         }

  50.         public int getAge() {
  51.                 return age;
  52.         }

  53.         public void setAge(int age) {
  54.                 this.age = age;
  55.         }

  56.         public int getHeight() {
  57.                 return height;
  58.         }

  59.         public void setHeight(int height) {
  60.                 this.height = height;
  61.         }

  62.         public int hashCode() {
  63.                 return this.getName().hashCode() + this.getAge();
  64.         }

  65.         public boolean equals(Object obj) {
  66.                 if (!(obj instanceof Person2)) {
  67.                         throw new RuntimeException("参数异常!");
  68.                 }
  69.                 Person2 p = (Person2) obj;
  70.                 return this.getAge() == p.getAge() && this.getHeight() == p.getHeight();
  71.         }
  72.         public String toString(){
  73.                 return "age:"+this.age+" height:"+height;
  74.         }
  75. }
复制代码

评分

参与人数 1技术分 +1 收起 理由
简★零度 + 1

查看全部评分

回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马