这是ArrayQueue的一种方法,可从ArrayQueue中删除所有重复项。 我知道newData是一个新数组(并且该语句应该将其初始化为我们原始数组(theData)的容量),我们复制要保留的值。我不了解(E [])部分的具体功能,如果有人可以解释,我将不胜感激,谢谢。
private E[] theData; // The dataarray
private int size; // The current size
private int capacity;// The current capacity
private int front; //front of the queue
private int rear; //rear of the queue
public void trimQueue(){
E[] newData = (E[]) new Object[capacity];
int i = front; //index for theData
int z = 0; //index for newData
for(int count=0; count<size; count++){
boolean found = false;
for(int k=0; k<size; k++){
if(theData[i] == newData[k]){found = true;}
}
if(!found){
newData[z] = theData[front];
}
i = (i+1) % capacity;
z++;
}
theData = newData;
size = z;
front = 0;
rear = size - 1;
}