需求

设计一个泛型集合类,提供public方法isEmpty、makeEmpty、insert、remove、isPresent、findMax、findMin方法。

代码结构

代码

测试类

package cn.edut.test1_14;

import java.util.Comparator;

public class Test_OrderCollection {
	public static void main(String[] args) {
		OrderCollection<Shape> oc = new OrderCollection<Shape>(new Shape[] {
				new Shape(1),
				new Square(3,1),
				new Circle(10)
		}) ;
		System.out.println("添加");
		oc.insert(new Circle(1));
		System.out.println(oc.size());
		System.out.println(oc.get(3).area);
		
		System.out.println("删除");
		System.out.println("删除的area:"+oc.remove(2).area);
		System.out.println(oc.size());
		System.out.println(oc.get(2).area);
		
		System.out.println("包含");
		System.out.println(oc.isPresentByEquals(new Shape(1)));
		System.out.println(oc.isPresentByComparable(new Shape(1)));
		
		System.out.println("最大");
		oc.insert(new Shape(100));
		System.out.println(oc.findMax().area); //get!
		System.out.println(oc.findMax(new Comparator<Shape>() {

			@Override
			public int compare(Shape o1, Shape o2) {
				System.out.println(o1.area +" - "+ o2.area);
				return (int) (o1.area - o2.area);
			}
			
		}).area); //get!!!
		
	}
}

泛型类

package cn.edut.test1_14;

import java.util.Comparator;

public class OrderCollection<T extends Comparable<? super T>> {
	Object[] objs ;
	public  OrderCollection(T[] objs){
		this.objs =  objs;
	}
	@SuppressWarnings("unchecked")
	public T get(int index) {
		return (T)objs[index];
	}
	
	public boolean isEmpty() {
		return objs.length == 0 ;
	}
	public void makeEmpty() {
		this.objs = new Object[0] ;
	}
	public int size() {
		return objs.length;
	}
	
	/** * index处插入o * @param o * @param index */
	public void insert(T o ) { //子类也能传入
		Object[] out = new Object[objs.length+1] ;
		System.arraycopy(objs, 0, out, 0, objs.length );
		out[objs.length] = o;
		objs = out;
	}
	
	/** * 删除index处的T * @param index * @return */
	public T remove(int index) {
		T oldValue = get(index);
		Object[] temp = new Object[objs.length-1];
		System.arraycopy(objs, 0, temp, 0, index);
		System.arraycopy(objs, index+1, temp, index ,temp.length-index);
		objs=temp;
		return oldValue;
	}
	
	/** * 包含。。默认比较方式, * @param x * @return */
	public boolean isPresentByEquals(T x) {
		for(int i=0 ; i<objs.length ; i++) {
			if( get(i).equals(x)) {
				return true;
			}
		}
		return false;
	}
	public boolean isPresentByComparable(T x) {
		for(int i=0 ; i<objs.length ; i++) {
			if(get(i).compareTo(x) == 0) {
				return true;
			}
		}
		return false;
	}
	
	/** * 找最大 * @return */
	public T findMax(){
		int maxIndex=0;
		for(int i=1 ; i<objs.length ; i++) {
			if(get(i).compareTo(get(maxIndex))>0) {
				maxIndex= i ;
			};
		}
		return get(maxIndex);
	}
	public T findMax(Comparator<? super T> c) {
		int maxIndex = 0 ; 
		for(int i=1 ; i<objs.length ; i++) {
			if(c.compare(get(i),get(maxIndex)) >0) {
				maxIndex = i;
			}
		}
		return get(maxIndex) ; 
	}
	
	
}

集合实例类(应该是这么叫吧?)

package cn.edut.test1_14;

public class Circle extends Shape{

	public Circle(double r) {
		super(r*r*Math.PI);
		// TODO Auto-generated constructor stub
	}
	
	

}

package cn.edut.test1_14;

public class Shape implements Comparable<Shape> {
	double area ;
	public Shape(double s) {
		this.area = s; 
	}
	@Override
	public boolean equals(Object obj) {
		if(super.equals(obj)) {
			return true;
		}
		Shape another = (Shape)obj;
		if(another.area == this.area){
			return true;
		}
		return false;
	}
	
	@Override
	public int compareTo(Shape o) {
		double temp =(this.area- o.area) ; 
		if(temp>1 || temp<-1) {
			return (int)temp;
		}else if(temp<0){
			return -1;
		}else if(temp>0) {
			return +1 ;
		}
		return  (int)temp ; //temp=0,-1,1
	}
}

package cn.edut.test1_14;

public class Square extends Shape {

	public Square(double l , double w) {
		super(l*w);
		// TODO Auto-generated constructor stub
	}
	
}