2017-04-07 59 views
-4

我已經給這個數組在類中用於賦值。如何用數組調用此方法?有可能嗎?

/** 
* 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. 
*/ 

該方法被標記爲私有,但我可以訪問它,因爲它在同一個類中。

任何幫助,這將不勝感激。

+0

問題是什麼? – dat3450

+0

_我已經給了這個數組在一個類中用於賦值._請給我們看這個類。 –

回答

0

cards數組應該是您的類中的實例變量。實例變量在類的方法之外聲明,通常在它的開頭。這些可以從該類的任何方法中調用。 This answer有一些關於實例變量的例子。

0

應該有另一種方法返回一個數組,該數組可能會調用方法compactHand();否則此方法不返回數組。 我已經宣佈了一些陣列的構造這可能有幫助

public class Test { 

    private String[] cards; 
    private int ncard; 

    public Test(String[] cards) { 
     this.cards = new String[cards.length]; 
     for(int i = 0; i <cards.length; i++){ 
      this.cards[i] = cards[i]; 
     } 

    } 

    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; 
    } 

    public static void main(String[] args) { 
     String[] cards = {"1", "2", "king", "ace"}; 
     Test test = new Test(cards); 
     test.compactHand(); 
     for (int i = 0; i < cards.length; i++) { 
      System.out.println(Arrays.toString(cards)); 
     } 

    } 

} 
相關問題