Java list容器


Java泛型编程,自己实现的list容器。在Java中提供大量的容器,便于满足用户的各种不同的需求在书写程序的时候,我们常常需要对大量的对象引用进行管理。为了实现有效的归类管理,我们常常将同类的引用放置在同一数据容器中。由于数据容器中存放了我们随时可能需要使用到的对象引用,所以一般的数据容器要都要能能提供方便的查询、遍历、修改等基本接口功能。Java容器类包含ListArrayListVector及map、HashTableHashMapHashset

该list容器是实现<extends Comparable <? super Anytype> > ,也就是说list容器中的类必须实现Comparable接口,用于实现比较大小sort。该list用内部类保存结点信息,next保存下一结点信息。该list实现init(),del(),sort(),toString(),getindex()。person类继承Comparable接口类,用于实现compareTo方法。

package list;
import java.lang.Comparable;
/*
 * list 是泛型编程,不提供具体的变量
 * 具体的变量里面提供具体的方法,比如 tostring() 或者 compare()
 * list是一个容器,里面不应该包含具体的方法 
 * */
public class list<Anytype extends Comparable<? super Anytype> > {   //<Anytype extends Comparable<? super Anytype>> 必须实现comparable接口
	@SuppressWarnings("hiding")         //内部类
	private class Node<Anytype>  {
		public Node<Anytype> next;
		public Anytype data;
		public Node(Anytype adata,Node<Anytype> anext){
			this.data=adata;
			this.next=anext;
		}
		public Node(){
			this.data=null;
			this.next=null;
		}
	}
	public static int size;           //静态变量  统计节点的数据
	private Node<Anytype> head;
	public list(){
		this.head=new Node<Anytype>();
		size=0;
	}
	public void add(Anytype item){
		Node<Anytype> anode=new Node<Anytype>(item,null);
		anode.next=head.next;
		head.next=anode;
		size++;
	}
	public boolean isempty(){
		if(size==0)
			return true;
		else 
			return false;
	}
	public boolean del(int index){
		if(index < 0 || index >size-1)
			throw new IndexOutOfBoundsException();   //抛出数组越界的异常
		Node<Anytype> delpre=new Node<Anytype>(null,null);
		delpre=head;
		for(int i=0;i<=index-1;i++){
			delpre=delpre.next;
		}
		Node<Anytype> cur=new Node<Anytype>(null,null);
		cur=delpre.next;
		delpre.next=cur.next;
		size--;
		return true;
	}
	public void sort(){
		Node<Anytype> p=new Node<Anytype>(null,null);
		Node<Anytype> q=new Node<Anytype>(null,null);
		Node<Anytype> r=new Node<Anytype>(null,null);
		Node<Anytype> s=new Node<Anytype>(null,null);
		p=head;
		q=head.next;
		while(q.next!=null){  //如果结点实现了comparTo的方法的话,必须要显式说明该方法的调用,说明该类必须实现了Comparable<? super Anytype> 的接口
			while(((Comparable<? super Anytype>) p.next.data).compareTo((Anytype)q.next.data)== -1){
				p=p.next;
				if(((Comparable<? super Anytype>) p.next.data).compareTo((Anytype)q.next.data)==0)
					break;
			}
			if(((Comparable<? super Anytype>) p.next.data).compareTo((Anytype)q.next.data)==1){
				s=q.next;
				q.next=s.next;
				r=p.next;
				p.next=s;
				s.next=r;
			}
			else
				q=q.next;
			p=head;
		}
				
	}
	public String toString(){
		String str = "[ ";
		if(isempty())
			str="list is empty";
		else{
		Node<Anytype> cur=new Node<Anytype>(null,null);
		cur=head.next;
		while(cur!=null){
			str+=cur.data.toString();    //Node 本身应该实现toString的方法
			if(cur.next!=null)
			   str+=",";
			cur=cur.next;
			}
		}
		str+=" ]";
		return str;
	}
	public Anytype getindex(int index){
		if(index < 0 || index >size-1)
			throw new IndexOutOfBoundsException();
		Node<Anytype> cur=new Node<Anytype>(null,null);
		cur=head.next;
		for(int i=0;i<index;i++){
			cur=cur.next;
		}
		return cur.data;            //getindex()应该实现返回结点
	}
}


class person implements Comparable<Object>{
	String name;
	int id;
    person(String d,int i){
		this.name=d;
		this.id=i;
	}
    int getid(){
    	return this.id;
    }
	@Override
	public int compareTo(Object o) {    //重载compareTo的方法
	    person p1=(person) o;
	    if(this.id > p1.id)
	    	return 1;
	    else if(this.id == p1.id)
	    	return 0;
	    else
            return -1;
	}
	public String toString(){
	    String	str="id="+id+" "+"name="+name;
		return str;
	}
    
	
}
class test{
	public static void main(String[] args) {
		list<person> tes=new list<person>();
		tes.add(new person("yang",2));
		tes.add(new person("li",1));
		tes.add(new person("zhao",7));
		tes.add(new person("zhou",3));
		System.out.println( tes.toString());
		tes.sort();
		person p=tes.getindex(0);
		System.out.println(p.toString());
		System.out.println(tes.toString());
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值