黑马程序员_java基础ArrayList实例

------<a href="http://www.itheima.com" target="blank">Java培训、Android培训、iOS培训、.Net培训</a>、期待与您交流! -------

import java.util.*;
/*
	去除ArrayList集合中的重复元素。
*/

class  ArrayListTest
public static void sop(Object obj)
{
	System.out.println(obj);
}
public static void main(String[] args)
{
	ArrayList al = new ArrayList();

	    al.add("java01");
		al.add("java02");
		al.add("java03");
		al.add("java04");
		al.add("java02");
		al.add("java03");
	
	 sop(al);
	 al = singleElement(al);
	 sop(al);
}


	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.add(obj);
/*
		Iterator it = al.iterator();

		while(it.hasNext())
			{
				sop(it.next()+"..."it.next());
			}
*/	
		
		}
	
	
	}
	
}
//存入人的基本无重复信息

class Person
{
	private String name;
	private int age;
	Person(String name,int age)
	{
		this.name = name;
		this.age = age;
	}
	public boolean equals(Object obj)
	{
		if(!(obj instanceof Person))
			return false;
		Person p = (Person)obj:
			return.this.name.equals(p.name) && this.age == p.age;
	
	}
	public String getName()
	{
		return name;
	}
	public int getAge()
	{
		return age;
	}
}
class ArrayListtTest2
{
	public static void sop(Object obj)
	{
		System.out.println(obj);
	}
	public static void main(String[] args)
	{
		ArrayList al = new ArrayList();
		al.add(new Person("lisi01",30));//al.add(Object obj);//Object obj = new Person("lisi01",30);
		al.add(new Person("lisi02",32));
		al.add(new Person("lisi03",33));
		al.add(new Person("lisi04",35));
		al.add(new Person("lisi03",33));
		al.add(new Person("lisi04",35));

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

		while (it.hasNext())
		{
			/*Object obj = it.next();
			Person p = (Person)obj;sop(it.next().getName()+"::"+it.next().getAge());*/
			Person p = (Person)it.next();
			sop(p.getName()+"::"+p.getAge());
		}
	}

}
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.add(obj);
/*
		Iterator it = al.iterator();

		while(it.hasNext())
			{
				sop(it.next()+"..."it.next());
			}
*/	
		
		}
	
	
	}
	

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值