黑马程序员技术交流社区
标题:
对象数组删除重复的一个问题
[打印本页]
作者:
zhkqy
时间:
2013-12-10 20:36
标题:
对象数组删除重复的一个问题
有这样一个数组如下
序号 姓名 年龄 身高
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做出来都可以。谢谢了!
作者:
Forevery
时间:
2013-12-10 21:31
import java.util.*;
class ListDemo
{
public static void main(String[] args)
{
ArrayList al = new ArrayList();
al.add(new Person("张三",10,170));
al.add(new Person("李四",20,150));
al.add(new Person("王五",20,150));
al.add(new Person("小六",22,184));
al.add(new Person("老二",24,178));
al.add(new Person("老四",23,182));
al.add(new Person("本帅",23,190));
al.add(new Person("赵启",23,182));
al = singleElement(al);
Iterator it = al.iterator();
while(it.hasNext())
{
Person p = (Person)it.next();
sop(p.getName()+".."+p.getAge()+":"+p.getHigh());
}
}
public static void sop(Object obj)
{
System.out.println(obj);
}
public static ArrayList singleElement(ArrayList al)
{
//定义一个临时容器。
ArrayList newAl = new ArrayList();
Iterator it = al.iterator();
while(it.hasNext())
{
Object obj = it.next();
if(!newAl.contains(obj))//判断newAl中是否包含该元素,没有的话就加上。
newAl.add(obj);
}
return newAl;
}
}
class Person
{
private String name;
private int age;
private int high;
Person(String name,int age,int high)
{
this.name = name;
this.age = age;
this.high = high;
}
public boolean equals(Object obj)
{
if(!(obj instanceof Person))
return false;
Person p = (Person)obj;
return this.age==p.age&&this.high==p.high;
}
public String getName()
{
return name;
}
public int getAge()
{
return age;
}
public int getHigh()
{
return high;
}
}
复制代码
除了人名没有改以外,其他要求都实现了。
建议用Collection.fill()去该名字~自己想去吧
作者:
范二青年
时间:
2013-12-10 21:38
public class Demo2 {
public static void main(String[] args) {
Student s1 = new Student("01", "张三", 10, 170);
Student s2 = new Student("02", "李四", 20, 175);
Student s3 = new Student("03", "马五", 20, 175);
Student s4 = new Student("04", "王六", 20, 175);
Student s5 = new Student("05", "孙七", 23, 180);
Student s6 = new Student("06", "陈八", 23, 180);
Student s7 = new Student("07", "赵九", 30, 160);
Student s8 = new Student("08", "吴十", 30, 160);
Student[] stus1 = {s1,s2,s3,s4,s5,s6,s7,s8};
for(int i = 0 ; i < stus1.length;i++){
for(int j = 0 ; j < stus1.length;j++){
if(i != j && stus1[i]!= null &&stus1[j] != null && stus1[i].equals(stus1[j])){
stus1[j]=null;
}
}
}
for(int i = 0 ; i < stus1.length;i++){
System.out.println(stus1[i] == null ? "" : stus1[i].toString());
}
}
}
class Student{
private String no;
private String name;
private int age;
private int height;
public Student(String no, String name, int age, int height) {
super();
this.no = no;
this.name = name;
this.age = age;
this.height = height;
}
public String getNo() {
return no;
}
public void setNo(String no) {
this.no = no;
}
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;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public boolean equals(Student stu) {
return (this.age+""+this.height).equals(stu.getAge()+""+stu.getHeight());
}
@Override
public String toString() {
return "Student [no=" + no + ", name=" + name + ", age=" + age
+ ", height=" + height + "]";
}
}
复制代码
实现的方法用的最简单无脑的,剩下的自己思考吧,如果看不懂找我给你加注释哈
作者:
末末
时间:
2013-12-10 22:25
public class Test2
{
public static void main(String[] args)
{
List<Person2> list=new ArrayList<Person2>();
List<Person2> list2 =new ArrayList<Person2>();
list.add(new Person2("01","one7",17,170));
list.add(new Person2("01","one5",17,170));
list.add(new Person2("01","one6",17,170));
list.add(new Person2("02","one2",17,170));
list.add(new Person2("03","one3",27,180));
list.add(new Person2("04","one4",37,160));
Iterator<Person2> it=list.iterator();
Iterator<Person2> it2=list2.iterator();
while(it.hasNext())
{
Person2 p=(Person2)it.next();
if(!list2.contains(p))
{
list2.add(p);
}
}
System.out.println(list2);
}
}
class Person2 {
private String num;
private String name;
private int age;
private int height;
Person2(String num, String name, int age, int height) {
this.num = num;
this.name = name;
this.age = age;
this.height = height;
}
public String getNum() {
return num;
}
public void setNum(String num) {
this.num = num;
}
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;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public int hashCode() {
return this.getName().hashCode() + this.getAge();
}
public boolean equals(Object obj) {
if (!(obj instanceof Person2)) {
throw new RuntimeException("参数异常!");
}
Person2 p = (Person2) obj;
return this.getAge() == p.getAge() && this.getHeight() == p.getHeight();
}
public String toString(){
return "age:"+this.age+" height:"+height;
}
}
复制代码
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2