我已經給這個數組在類中用於賦值。如何用數組調用此方法?有可能嗎?
/**
* compactHand - THIS METHOD IS SUPPLIED FOR YOU!
* re-order the array so that all cards occupy the early indices, i.e. 0,1,2,...
* and that all the nulls are in the higher indices.
* Also count the number of cards in the array and set ncard accordingly.
* Example: if array is [Kclub][null][2diamond][null][7spade]
* then this routine sets array to [Kclub][2diamond][7spade][null][null] and sets ncard to 3.
* Note that the relative order of cards is not changed.
*/
private void compactHand(){
int nc=0, ix=0, nullix=-1;
// find first null
do{
if (cards[ix]==null){
nullix = ix;
break;
}
} while (++ix<cards.length);
if (nullix==-1){
// there were no nulls. set ncards and return.
ncard = cards.length;
return;
}
nc = nullix;
// loop to end of array and swap cards for nulls
for(ix=nullix; ix<cards.length; ix++)
if (!(cards[ix]==null)){
cards[nullix++] = cards[ix];
cards[ix] = null;
nc++;
}
ncard = nc;
}
我已經完成了大部分的任務,但對於的方法之一,我需要在一個陣列命名爲卡內它調用此方法。我試着用調用它:
cards.compactHand();
該方法不接受數組作爲參數,但我需要完成的方法,說明具體說明來使用它。
/**
* delete Cards at specified indices and compact the array.
* if any indices do not correspond to cards then the array must be unchanged
* and this member returns false.
* otherwise the specified cards are deleted, the array compacted and true returned.
* @param index index of array element to delete
* @return true if all elements successfully deleted, false if none deleted.
*/
該方法被標記爲私有,但我可以訪問它,因爲它在同一個類中。
任何幫助,這將不勝感激。
問題是什麼? – dat3450
_我已經給了這個數組在一個類中用於賦值._請給我們看這個類。 –