设计一个泛型类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());
} }

  

  

最新文章

  1. 用JMeter测试monggodb的请求
  2. ABP理论学习之数据过滤器
  3. java &quot;&quot;.split(&quot;,&quot;)
  4. lamp安装
  5. Linux autojump命令
  6. php bom \ufeff
  7. Nexus私服忘记用户名密码,Nexus私服如何找回用户名密码
  8. mysql及php命名规范
  9. ios开发APP必须要了解的基本配置
  10. arcgis中的 style和serverstyle
  11. IKVM - 0.42.0.3 .NET平台上的Java实现
  12. Keil编译后的各文件介绍
  13. gitlab使用入门
  14. Java连接数据库(mysql,sqlserver)
  15. Jquery操作Table
  16. 换行符 &#39;\n&#39; 和 回车符 &#39;\r&#39; 的区别?
  17. Automatically migrating data to new machines kafka集群扩充迁移topic
  18. Visitor模式和Observer观察者模式
  19. android基础----&gt;service的生命周期
  20. xml声明中的standalone属性

热门文章

  1. 分布式缓存Memcached---开篇的话
  2. QT 调用VS2015编写的Dll
  3. iOS 如何在Label中显示html的文本
  4. CSS中可以和不可以继承的属性
  5. java 反射机制
  6. IE环境下判断IE版本的语句...[if lte IE 6]……[endif][if lte IE 7]……[endif]
  7. DB2 常用命令小结
  8. html5,表单的综合案例
  9. Java中的回调函数
  10. uexWeiXin插件