我是將數組封裝爲類的。 其中方法說明:getmax(){ //計算數據個數。getData(int index)得到index的元素。setData(int index,int b)設置索引爲index的元素值爲b.
public void noDup(){
//循環嵌套將重複的設爲-1
int max = this.getmax();
for(int i = 0;i < max;i ++){
for(int j = i + 1;j < max;j ++ ){
if (this.getData(i) == this.getData(j)) {
this.setData(i, -1);
}
}
}
}
//刪除value爲x的所有項
public void dele(int x){
int i = 0;
int max = this.getmax();
while(!(this.getData(i) == 0)){
if (this.getData(i) == x) {
for(int j = i;j < max;j ++){
this.setData(j, this.getData(j + 1));
}
}else{
i++;
}
}
}