zoukankan      html  css  js  c++  java
  • 设计一个泛型类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());
    	}
    
    }
    

      

      

  • 相关阅读:
    Windows Server 2008取消登录前的Ctrl+Alt+Delete组合键操作
    Kali Linux远程连接Windows服务器
    Kali Linux虚拟机安装完整安装过程及简单配置(视频)
    Kali Linux中下载工具Axel的安装和使用
    2017年Kali Linux更新源
    解决VMware虚拟机报错“无法连接MKS:套接字连接尝试次数太多,正在放弃”
    .deb软件包的安装和软件的卸载
    解决C语言程序报错:return type defaults to‘int’
    解决BackBox中Fcitx输入法中文输入状态下不显示候选词框的问题
    导航狗信息导航网站首页源代码(2017年11月03日版)
  • 原文地址:https://www.cnblogs.com/wangchaoBlog/p/6076904.html
Copyright © 2011-2022 走看看