2012-08-22 124 views
0

什麼是正確的方法來使一個類中的不同方法可以訪問構造函數的參數?構造函數的參數可從另一種方法訪問?

例如,在下面的代碼片段,我要讓ň一個名爲amethod方法方法中訪問,而無需改變amethod方法的現有說法簽名。 myArray.length是最好的選擇嗎?

public class MyClass{ 

    private int[][] myArray; 

    public MyClass(int N){ 

    if(N <= 0) 
     throw new IndexOutOfBoundsException("Input Error: N <= 0"); 

    myArray = new int[N][N];    
    } 

    public void aMethod(int i, int j){ 

    // N won't work here. Is myArray.length the best alternative?  
    if(i <= 1 || i > N) 
     throw new IndexOutOfBoundsException("Row index i out of bounds"); 
    if(j <= 1 || j > N) 
     throw new IndexOutOfBoundsException("Column index j out of bounds");    
    } 
} 

EDIT 1 我測試,用於輸入大於0,所以如果用戶對於i或0進入0對於j,輸入無效更大。

+1

將它存儲在一個字段? – Vlad

回答

5

只需爲它創建一個字段,就像您對數組所做的一樣。

public class MyClass{ 

    private int[][] myArray; 
    private int myArraySize; 

    public MyClass(int N){ 

     if(N <= 0) 
     throw new IndexOutOfBoundsException("Input Error: N <= 0"); 

     myArray = new int[N][N]; 
     myArraySize = N;    
    } 

    ... 
} 

然而,在這種情況下,我不會那樣做,我會改變amethod方法()代替:

public void aMethod(int i, int j){ 

    // N won't work here. Is myArray.length the best alternative  
    if(i < 0 || i >= myArray.length) 
     throw new IndexOutOfBoundsException("Index i out of bounds"); 
    if(j < 0 || j >= myArray[i].length) 
     throw new IndexOutOfBoundsException("Column index j out of bounds");    
} 

(我也改變了檢查,以便[0..N-1]而不是[1..N],因爲數組是從0開始索引的。)

+0

+1感謝您的兩種選擇。 @Bohemian和PeterLawrey建議使用myArraySize的最後一個關鍵字 – Anthony

1

看起來'N'應該存儲在類中的成員中。如果你這樣做了,那麼它也可以通過aMethod()方法訪問。在任何情況下,您都應該在構造函數中調用需要構造函數參數的方法,或將這些構造函數參數存儲在成員變量中,並使其可用於其他方法。

2

創建(和上帝的份上命名爲使用通常的命名約定)的字段:

public class MyClass{ 

    private int[][] myArray; 
    private final int n; // it should be final, because the array has the same dimension 

    public MyClass(int n){ 
    this.n = n; 
    // other stuff 
    } 

    public void aMethod(int i, int j){ 
    // use n here 
    } 
} 
+0

+1非常感謝提及最終關鍵字 – Anthony

1

我認爲IndexOutOfBoundsException會沒有你的注意,因爲Java的檢查數組邊界在運行時被拋出。你確定你需要這個額外的支票嗎?

+1

瞭解兩個索引中的哪一個出界是很好的。 – biziclop

3

爲什麼不使用length數組myArray.length

+0

+1是的你是對的。但有一點聲音告訴我,myArray.length更像是一種破解,我想知道解決這個問題的最好方法是什麼。 – Anthony

1

在你的類中添加一個新的領域,像我一樣n大小

public class MyClass{ 

    private int[][] myArray; 
    private int nSize; 

    public MyClass(int N){ 

    if(N <= 0) 
     throw new IndexOutOfBoundsException("Input Error: N <= 0"); 

    myArray = new int[N][N]; 
    this.nSize= N;    
} 
3

你可以將其存儲爲另一個字段,但是它已經存儲。

public class MyClass{ 

    private final int[][] myArray; 

    public MyClass(int n){ 
    myArray = new int[n][n]; // will throw an exception if N < 0. 
    } 

    public void aMethod(int i, int j){ 
    int n = myArray.length; 

    if(i < 0 || i >= n) 
     throw new IndexOutOfBoundsException("Index i out of bounds"); 
    if(j < 0 || j >= n) 
     throw new IndexOutOfBoundsException("Column index j out of bounds");    
    } 
} 

當然索引0和1對數組有效。如果你沒有執行這些檢查,你會得到一個IndexOutOfBoundException,但它會告訴你什麼是無效的值可能是有用的。

+0

+1哇!這是優雅的! – Anthony

+0

我是Java新手,但你的代碼片段顯示了波蘭和經驗。再次感謝。 – Anthony

+0

@Anthony隨着103k代表,你會希望如此。;) –

相關問題