----------------------------------------------------------------------------- android培训、java培训、期待与您交流! --------------------------------------------------------------------------------
1. 使用LinkedList模拟一个堆栈或者队列数据结构。
import java.util.*;
class Queue
{
private LinkedList link;
Queue()
{
link = new LinkedList();
}
public void myAdd(Object obj)
{
link.addFirst(obj);
}
public Object myGet()
{
return link.removeFirst();
}
public boolean isNull()
{
return link.isEmpty();
}
}
class LinkedListTest
{
public static void main(String[] args)
{
Queue q = new Queue();
q.myAdd("java01");
q.myAdd("java02");
q.myAdd("java03");
q.myAdd("java04");
while(!q.isNull())
{
System.out.println(q.myGet());
}
}
}
2. 去除ArrayList集合中的重复元素。
import java.util.*;
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("java01");
al.add("java02");
al.add("java01");
// al.add("java03");
/*
在迭代时循环中next调用一次,就要hasNext判断一次。
Iterator it = al.iterator();
while(it.hasNext())
{
sop(it.next()+"...."+it.next());
}
*/
/**/
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);
}
return newAl;
}
}
3. 将自定义对象作为元素存到ArrayList集合中,并去除重复元素。
比如:存人对象,同姓名同年龄,视为同一个人。为重复元素。
/*
将自定义对象作为元素存到ArrayList集合中,并去除重复元素。
比如:存人对象,同姓名同年龄,视为同一个人。为重复元素。
思路:
1. 对人描述,将数据封转进人对象。
2. 定义容器,将人存入。
3. 取出。
List集合判断元素是否相同,依据的是元素的equals方法。
*/
import java.util.*;
class Person
{
private String name;
private int age;
Person(String name , int age)
{
this.name = name;
this.age = age;
}
public String getName()
{
return name;
}
public int getAge()
{
return age;
}
public boolean equals(Object obj)
{
if(!(obj instanceof Person))
return false;
Person p = (Person)obj;
System.out.println(this.name+"...."+p.name);
return this.name.equals(p.name) && this.age == p.age;
}
}
class ArrayListTest2
{
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(new Person("lisi02",32));
al.add(new Person("lisi02",32));
al.add(new Person("lisi03",33));
al.add(new Person("lisi04",35));
al.add(new Person("lisi04",35));
al = singleElement(al);
Iterator it = al.iterator();
while (it.hasNext())
{
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);
}
return newAl;
}
}
4. 往hashSet集合中存入自定对象
姓名和年龄相同为同一个人,重复元素。
import java.util.*;
class HashSetTest
{
public static void sop(Object obj)
{
System.out.println(obj);
}
public static void main(String[] args)
{
HashSet hs = new HashSet();
hs.add(new Person("a1",11));
hs.add(new Person("a2",12));
hs.add(new Person("a3",13));
// hs.add(new Person("a2",12));
// hs.add(new Person("a4",14));
//sop("a1:"+hs.contains(new Person("a2",12)));
// hs.remove(new Person("a4",13));
Iterator it = hs.iterator();
while(it.hasNext())
{
Person p = (Person)it.next();
sop(p.getName()+"::"+p.getAge());
}
}
}
class Person
{
private String name;
private int age;
Person(String name,int age)
{
this.name = name;
this.age = age;
}
public int hashCode()
{
System.out.println(this.name+"....hashCode");
return name.hashCode()+age*37;
}
public boolean equals(Object obj)
{
if(!(obj instanceof Person))
return false;
Person p = (Person)obj;
System.out.println(this.name+"...equals.."+p.name);
return this.name.equals(p.name) && this.age == p.age;
}
public String getName()
{
return name;
}
public int getAge()
{
return age;
}
}
5. 往TreeSet集合中存储自定义对象学生。
想按照学生的年龄进行排序。
/*
需求:
往TreeSet集合中存储自定义对象学生。
想按照学生的年龄进行排序。
思路:
实现Comparable,覆写compareTo方法。
判断主要条件及次要条件。
*/
import java.util.*;
class TreeSetDemo
{
public static void main(String[] args)
{
TreeSet ts = new TreeSet();
ts.add(new Student("lisi02",22));
ts.add(new Student("lisi007",20));
ts.add(new Student("lisi09",19));
ts.add(new Student("lisi01",40));
ts.add(new Student("lisi08",19));
for (Iterator it = ts.iterator(); it.hasNext(); )
{
Student stu = (Student)it.next();
System.out.println(stu.getName()+"..."+stu.getAge());
}
}
}
class Student implements Comparable//该接口强制让学生具备比较性
{
private String name;
private int age;
Student(String name,int age)
{
this.name = name;
this.age = age;
}
//覆盖Comparable中的compareTo方法
public int compareTo(Object obj)
{
//先判断obj是不是学生的一个实例,如果不是,抛出一个异常
if(!(obj instanceof Student))
throw new RuntimeException("不是学生对象");
//强制转换
Student s = (Student)obj;
System.out.println(this.name+"....compareto...."+s.name);
//当此对象大于指定对象,返回正数
if(this.age>s.age)
return 1;
//排序时,当主要条件相同时,还要判断一下次要条件。
if(this.age==s.age)
{
return this.name.compareTo(s.name);
}
return -1;
}
public String getName()
{
return name;
}
public int getAge()
{
return age;
}
}
6. 照字符串长度排序。
/*
练习:按照字符串长度排序
思路:
字符串本身具备比较性,但是它的比较方式不是所需要的。
这是就只能使用比较器。
步骤:
用一个类去实现接口。
比较主要条件和次要条件。
*/
import java.util.*;
class TreeSetTest
{
public static void main(String[] args)
{
TreeSet ts = new TreeSet(new StrLenComparator());
ts.add("abcd");
ts.add("cc");
ts.add("cba");
ts.add("aaa");
ts.add("z");
ts.add("hahaha");
for (Iterator it = ts.iterator(); it.hasNext(); )
{
System.out.println(it.next());
}
}
}
class StrLenComparator implements Comparator
{
//覆写Comparator中的compare方法
public int compare(Object o1,Object o2)
{
//强转
String s1 = (String)o1;
String s2 = (String)o2;
int num = new Integer(s1.length()).compareTo(new Integer(s2.length()));
if(num==0)
return s1.compareTo(s2);
return num;
}
}
----------------------------------------------------------------------------- android培训、java培训、期待与您交流!--------------------------------------------------------------------------------
详细请查看:http://edu.youkuaiyun.com/heima/