Java泛型編程,自己實現的list容器。在Java中提供大量的容器,便於滿足用戶的各種不同的需求在書寫程序的時候,我們常常需要對大量的對象引用進行管理。爲了實現有效的歸類管理,我們常常將同類的引用放置在同一數據容器中。由於數據容器中存放了我們隨時可能需要使用到的對象引用,所以一般的數據容器要都要能能提供方便的查詢、遍歷、修改等基本接口功能。Java容器類包含List、ArrayList、Vector及map、HashTable、HashMap、Hashset。
該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());
}
}