实现一个基于LinkedList的队列数据结构,去除ArrayList集合中重复的元素,

来源:互联网 发布:linux启动oracle 编辑:程序博客网 时间:2024/05/21 07:57
/*
需求:实现一个基于LinkedList的队列数据结构
构造方法:
              LinkedList() 
                      构造一个空列表。 
              LinkedList(Collection<? extends E> c) 
                      构造一个包含指定 collection 中的元素的列表,这些元素按其 collection 的迭代器返回的顺序排列。


特有方法:
         boolean offer(E e) 
                  将指定元素添加到此列表的末尾(最后一个元素)。 
         boolean offerFirst(E e) 
                 在此列表的开头插入指定的元素。 
         boolean offerLast(E e) 
                  在此列表末尾插入指定的元素。 
         E peek() 
                  获取但不移除此列表的头(第一个元素)。 
         E peekFirst() 
                   获取但不移除此列表的第一个元素;如果此列表为空,则返回 null。 
         E peekLast() 
                   获取但不移除此列表的最后一个元素;如果此列表为空,则返回 null。 
         E poll() 
                   获取并移除此列表的头(第一个元素) 
        E pollFirst() 
                 获取并移除此列表的第一个元素;如果此列表为空,则返回 null。 
        E pollLast() 
                 获取并移除此列表的最后一个元素;如果此列表为空,则返回 null。 
 


*/


//导入util包
import java.util.*;
//定义一个带泛型的Queue类
class Queue<Object>
{
       private LinkedList<Object> link;
       //Queue的构造方法,一初始化就创建一个LinkedList对象
       Queue()
       {
             link = new LinkedList<Object>();
       }
       public void myAdd(Object obj)
       {
              link.offerFirst(obj);
       }
       public Object myGet()
       {
             return link.removeLast();//这里用removeLast()方法是因为remove方法取出且删除元素方法
       }
       public boolean isNull()
       {
             return link.isEmpty();
       }
}


class LinkedListTest
{
         public static void main(String[] args)
         {
                    Queue<String> q = new Queue<String>();
                    q.myAdd("java01");
                     q.myAdd("java01");
                     q.myAdd("java01");
                    q.myAdd("java01");
                    while(!q.isNull())
                    {
                            System.out.println(q.myGet());
                    }
         }

}




/*
需求:去除ArrayList集合中重复的元素,
必须得重写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;
                 System.out.println(this.name+"......."+this.age);
                 Person p = (Person)obj;
                 return this.name.equals(p.name)&&this.age==p.age;//equals和重写的equals方法不一样,这个是String类的equals方法
         }
}


class ArrayListDemo2
{
          public static void main(String[] args)
          {
                 ArrayList al = new ArrayList();
                 al.add(new Person("张三",21));
                 al.add(new Person("李四",23));
                 al.add(new Person("李四",23));
                 al.add(new Person("王五",24));
                 al.add(new Person("赵六",25));
                 al.add(new Person("赵六",25));
                 al.add(new Person("周七",26));
                 al.add(new Person("胡八",27));
                 al.add(new Person("胡八",27));
               
               al = Element(al);
               for(Iterator it = al.iterator(); it.hasNext(); )
                {
                        Person p = (Person)it.next();//必须向下转型,因为getName()和getAge()方法都是我们在Person类中定义的
                        System.out.println(p.getName()+"===="+p.getAge());
                }
          }
           public static ArrayList Element(ArrayList al)
             {
                       ArrayList newal = new ArrayList();
                       for(Iterator it = al.iterator(); it.hasNext(); )
                       {
                                  Object obj = it.next();
                                  if(!newal.contains(obj))
                                         newal.add(obj);
                       }
                       return newal;
             }
}






















0 0
原创粉丝点击