首页 > 代码库 > 模拟java.util.Collection一些简单的用法

模拟java.util.Collection一些简单的用法

/*需求:模拟java.util.Collection一些简单的用法!注意:java虚拟机中并没有泛型类型的对象。泛型是通过编译器执行一个被称为类型擦除的前段转换来实现的。1)用泛型的原生类型替代泛型。原生类型是泛型中去掉尖括号及其中的类型参数的类或接口。泛型中所有对类型变量的引用都替换为类型变量的最近上限类型,如对于Cage<T extends Animal>, T的引用将用Animal替换,而对于Cage<T>, T 的引用将用Object替换。2)*/interface Collection<T>{    public abstract void add(T o);	public abstract Object getElement(int index);	public abstract void setElement(int index, T o);	public abstract Object[] toArray();	public abstract boolean isNext();	public abstract Object nextElemenst();}class Array<T> implements Collection<T>{	 int buildNum = 10;	 int maxSize = 10;	 int size = 0;	 int flag = 0;	 int xIndex;     Object[] elementsData = http://www.mamicode.com/new Object[maxSize];"unchecked")		if(from >= to) return;		int mid = (from+to)/2;        Object pivotkey = arr[from];		if(arr[from] instanceof Comparable){		   System.out.println("dfsdfsdf");		   Comparable pp = (Comparable)arr[from];		}		if( ((Comparable)arr[mid]).compareTo(arr[from]) * ((Comparable)arr[mid]).compareTo(arr[to])<0){		    arr[from] = arr[mid];			arr[mid] = pivotkey; 		}		else if( ((Comparable<Object>)arr[to]).compareTo(arr[from]) * ((Comparable<Object>)arr[to]).compareTo(arr[mid])<0){		    arr[from] = arr[to];			arr[to] = pivotkey;		}		pivotkey = arr[from];		int low = from, high = to;		while(low < high){		    while(low < high && ((Comparable<Object>)arr[low]).compareTo(pivotkey)>0)  ++low;		    while(low < high && ((Comparable<Object>)arr[high]).compareTo(pivotkey)<0)  --high;						if(low < high){			   Object tmp = arr[low];			   arr[low] = arr[high];			   arr[high] = tmp;			}		}		 arr[low] = pivotkey;		 qsort(arr, from, low-1);		 qsort(arr, low+1, to);     } }class Person implements Comparable<Person>{   String name;   int age;   Person(){       name = "";	   age = 0;   }   Person(String name, int age){       this.name = name;	   this.age = age;   }   public String toString(){       return name + "...." + age;   }      public boolean equals(Object o){       Person x = (Person)o; 	   return name.equals(x.name) && age==x.age;   }       public int compareTo(Person o){       if(name.compareTo(o.name)==0)	      return age - o.age;	   return name.compareTo(o.name);   }}class ArrayList<T>{     Object elementsData[] = null;	 int size = 0;     ArrayList(Collection<T> x){	     elementsData = http://www.mamicode.com/x.toArray();"h" + i + 1, i+1));		   		System.out.println("Array 中的数据。。。。。");		while(arr.isNext()){		    System.out.println( ((Person)arr.nextElemenst()) );		}		System.out.println(arr.getIndex(new Person("h101", 11)));		System.out.println(arr.getElement(5));		System.out.println("*******************************************************\nArrayList中排好序的数据!");				ArrayList<Person> arrT = new ArrayList<Person>(arr);		arrT.MaxToMinSort();		arrT.output();    }}