1. 程式人生 > >設計一個泛型類orderedCollection

設計一個泛型類orderedCollection

ble test println 一個 不為 move stat arrays this

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