标签:blog java os 数据 io for cti div
/* 需求:模拟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 = new Object[maxSize]; /* 判断时候数组大小不够 */ public void isFullSize(){ if(maxSize == size){ Object[] tmp = new Object[maxSize += buildNum]; for(int i=0; i<size; ++i) tmp[i] = elementsData[i]; elementsData = tmp; } } //添加元素 public void add(T o){ isFullSize(); elementsData[size++] = o; } //得到某一个下标的元素 public Object getElement(int index){ if(index < size){ System.out.println(elementsData[index].getClass().getName()); return elementsData[index]; } else return null; } //获取某一个元素的下标 public int getIndex(T o){ for(int i=0; i<size; ++i) if(o.equals(elementsData[i])) return i; return -1; } //设置某个元素为一新值 public void setElement(int index, T o){ if(index < size) elementsData[index] = o; } //放回对象数组 public Object[] toArray(){ return elementsData; } //是否有下一个元素 public boolean isNext(){ if(flag == 0){ flag =1; xIndex=0; } if(xIndex == size){ flag = 0; return false; } return true; } //返回下一个元素 public Object nextElemenst(){ return elementsData[xIndex++]; } public static void qsort(Object arr[], int from, int to){ //@SuppressWarnings("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 = x.toArray(); for(int i=0; i<elementsData.length; ++i){ if(elementsData[i]==null) break; ++size;//不计入空引用 } } public void MaxToMinSort(){//从大到小排序 Array.qsort(elementsData, 0, size-1); } public void output(){ for(int i=0; i<size; ++i) System.out.println(elementsData[i]); } } public class Test{ public static void main(String[] args){ Array<Person>arr = new Array<Person>(); for(int i=0; i<15; ++i) arr.add(new Person("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(); } }
模拟java.util.Collection一些简单的用法,布布扣,bubuko.com
标签:blog java os 数据 io for cti div
原文地址:http://www.cnblogs.com/hujunzheng/p/3872828.html