设计一个泛型类orderedCollection
Posted 暗时间&量变
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了设计一个泛型类orderedCollection相关的知识,希望对你有一定的参考价值。
import java.util.Arrays;
/**
* 设计一个泛型类orderedCollection,它存储的Comparable对象的集合(在数组中),
* 以及该集合的当前大小。提供public方法isEmpty,makeEmpty,insert,remove,findMin和
* findMax。finfMin和findMax分别返回该集合中最小的和最大T对象的引用(如果该集合为空,则返回null)
* @author wulei
*
* @param <E>
*/
public class CollectionTest<E> {
private Comparable[] elementData;
public void setElementData(Comparable[] obj){
this.elementData = obj;
}
public Comparable[] getElementData(){
return elementData;
}
public boolean isEmpty(){
return elementData.length == 0;
}
/**
* makeEmpty与List的clear()方法类似,Collection不为null,但是元素个数为0
*/
public void makeEmpty(){
elementData = new Comparable[]{};
}
public void insert(Comparable obj){
int length = elementData.length;
Comparable[] temp = new Comparable[length+1];
System.arraycopy(elementData, 0, temp, 0, length);
temp[length] = obj;
elementData = temp;
}
public void remove(int index){
if(index < 0 || elementData == null){
return ;
}
int length = elementData.length;
if((length-1) < index){
return ;
}
Comparable[] temp = new Comparable[length-1];
System.arraycopy(elementData, 0, temp, 0, index);
System.arraycopy(elementData, index+1, temp, index, length-index-1);
elementData = temp;
}
public void remove(E obj){
if(elementData == null || obj == null){
return;
}
int length = elementData.length;
for (int i = 0; i < length; i++) {
if(elementData[i] != null && elementData[i] == obj){
Comparable[] temp = new Comparable[length-1];
System.arraycopy(elementData, 0, temp, 0, i);
System.arraycopy(elementData, i+1, temp, i, length-i-1);
elementData = temp;
break;
}
}
}
public boolean isPresent(E obj){
if(elementData == null || obj == null){
return false;
}
boolean flag = false;
for (int i = 0; i < elementData.length; i++) {
if(null != elementData[i] && obj == elementData[i]){
flag = true;
break;
}
}
return flag;
}
public Comparable<E> findMin() {
if(elementData == null || elementData.length == 0){
return null;
}
int index = 0;
for (int i = 0; i < elementData.length; i++) {
if(elementData[i].compareTo(elementData[index]) < 0){
index = i;
}
}
return elementData[index];
}
public Comparable<E> findMax(){
if(elementData == null || elementData.length == 0){
return null;
}
int index = 0;
for (int i = 0; i < elementData.length; i++) {
if(elementData[i].compareTo(elementData[index]) > 0){
index = i;
}
}
return elementData[index];
}
public static void main(String[] args){
int i =0;
CollectionTest<String> collectionTest = new CollectionTest<String>();
Comparable[] objects =new Comparable[]{"9"};
collectionTest.setElementData(objects);
collectionTest.insert("1");
collectionTest.insert("2");
collectionTest.insert("3");
collectionTest.insert("4");
System.err.println(Arrays.toString(collectionTest.getElementData()));
collectionTest.remove(0);
System.err.println(Arrays.toString(collectionTest.getElementData()));
collectionTest.remove("2");
System.err.println(Arrays.toString(collectionTest.getElementData()));
boolean flag = collectionTest.isEmpty();
System.err.println("isEmpty: "+flag);
boolean flag1 = collectionTest.isPresent("1");
System.err.println("isPresent: "+flag1);
//collectionTest.makeEmpty();
boolean flag2 = collectionTest.isEmpty();
System.err.println("isEmpty: "+flag2);
System.err.println("min: "+collectionTest.findMin());
System.err.println("max: "+collectionTest.findMax());
}
}
以上是关于设计一个泛型类orderedCollection的主要内容,如果未能解决你的问题,请参考以下文章