• 设计一个泛型类orderedCollection


    设计一个泛型类orderedCollection,它存储的Comparable对象的集合(在数组中),以及该集合的当前大小。提供public方法isEmpty,makeEmpty,insert,remove,findMin和 findMax。

    finfMin和findMax分别返回该集合中最小的和最大T对象的引用(如果该集合为空,则返回null)

    /**
     * <p>
     * 设计一个泛型类orderedCollection,它存储的Comparable对象的集合(在数组中),
     * 以及该集合的当前大小。提供public方法isEmpty,makeEmpty,insert,remove,findMin和
     * findMax。finfMin和findMax分别返回该集合中最小的和最大T对象的引用(如果该集合为空,则返回null)
     * </p>
     *
     * @author wangchao
     *
     * @version 1.0.0
     *
     * @since 1.0.0
     *
     */
    public class OrderedCollection {
    	private Comparable[] obj;
    	private int length;
    
    	public int getLength() {
    		return obj.length;
    	}
    
    	public void setLength(int length) {
    		this.length = length;
    	}
    
    	public Comparable[] getObj() {
    		return obj;
    	}
    
    	public void setObj(Comparable[] obj) {
    		this.obj = obj;
    	}
    
    	public void makeEmpty() {
    		obj = new Comparable[] {};
    	}
    
    	public Comparable findMin() {
    		if (obj.length == 0) {
    			return null;
    		}
    		int min = 0;
    		for (int i = 1; i < obj.length; i++) {
    			if (obj[i].compareTo(obj[min]) <= 0) {
    				min = i;
    			}
    		}
    		return obj[min];
    	}
    
    	public Comparable findMax() {
    		if (obj.length == 0) {
    			return null;
    		}
    		int max = 0;
    		for (int i = 1; i < obj.length; i++) {
    			if (obj[i].compareTo(obj[max]) > 0) {
    				max = i;
    			}
    		}
    		return obj[max];
    	}
    
    	public boolean isEmpty() {
    		return obj.length > 0 ? false : true;
    	};
    
    	public void insert(Comparable o) {
    		// 扩展数组容量
    		Comparable[] temp = new Comparable[obj.length + 1];
    		// 拷贝原有数组
    		for (int i = 0; i < obj.length; i++) {
    			temp[i] = obj[i];
    		}
    		// 末位添加新元素
    		temp[obj.length] = o;
    		obj = temp;
    	}
    
    	public boolean isPresent(Comparable o) {
    		if (obj.length == 0) {
    			return false;
    		}
    		// 遍历判断
    		for (Comparable ob : obj) {
    			if (o.equals(ob))
    				return true;
    		}
    		return false;
    	}
    
    	/**
    	 * <p>
    	 * 此处写的很复杂,应该有更简单的方法实现
    	 * </p>
    	 */
    	public void remove(Comparable o) {
    		if (obj.length == 0) {
    			return;
    		}
    		int count = 0;
    		for (int i = 0; i < obj.length; i++) {
    			if (o.equals(obj[i])) {
    				obj[i] = null;
    				count++;
    			}
    		}
    
    		Comparable[] temp = new Comparable[obj.length - count];
    		int i = 0;
    		for (Comparable ob : obj) {
    			if (ob != null) {
    				temp[i] = ob;
    				i++;
    			}
    		}
    		obj = temp;
    	}
    
    	public static void main(String[] args) {
    		OrderedCollection oc = new OrderedCollection();
    		Comparable[] obj = new Comparable[] { 12, 4, 6, 2, 68 };
    		oc.setObj(obj);
    		System.err.println(oc.findMin());
    		System.err.println(oc.getLength());
    	}
    
    }
    

      

      

  • 相关阅读:
    递归实现随机数不重复问题
    今天写的一个工厂工具类
    Win7 x64 IIS运行ASP+Access故障完美解决方法(转)
    li中,标题和日期一排,且日期靠右
    [学习笔记] extends implements 的区别与联系 [转载]
    [学习笔记] vim使用大全 [转]
    MidPoint Displacement for Terrain Rendering
    CryEngine3 打造另一个真实世界
    Hello C++ AMP!
    DetailMap For Terrain Rendering
  • 原文地址:https://www.cnblogs.com/wangchaoBlog/p/6076904.html
Copyright © 2020-2023  润新知