首页 > 代码库 > 设计一个泛型类orderedCollection

设计一个泛型类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 = http://www.mamicode.com/obj;
}

public Comparable[] getElementData(){
return elementData;
}


public boolean isEmpty(){
return elementData.length == 0;
}

/**
* makeEmpty与List的clear()方法类似,Collection不为null,但是元素个数为0
*/
public void makeEmpty(){
elementData = http://www.mamicode.com/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 = http://www.mamicode.com/temp;
}

public void remove(int index){
if(index < 0 || elementData =http://www.mamicode.com/= 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 = http://www.mamicode.com/temp;
}

public void remove(E obj){
if(elementData =http://www.mamicode.com/= 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 = http://www.mamicode.com/temp;
break;
}
}
}

public boolean isPresent(E obj){
if(elementData =http://www.mamicode.com/= 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 =http://www.mamicode.com/= 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 =http://www.mamicode.com/= 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