0

クラス内のさまざまなメソッドがコンストラクターの引数にアクセスできるようにする正しい方法は何ですか?

たとえば、以下のコードスニペットでは、aMethodの既存の引数シグネチャを変更せずに、aMethodというメソッド内でNにアクセスできるようにします。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");            
  }
}

編集1 私は0より大きい入力をテストしているので、ユーザーがiに0を入力するか、jに0を入力すると、入力は無効になります。

4

7 に答える 7

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からインデックス付けされるため、[1..N]ではなく[0..N-1]を許可するようにチェックも変更しました。)

于 2012-08-22T11:44:08.763 に答える
3

なぜlength配列を使わないのmyArray.length ですか?

于 2012-08-22T11:48:05.617 に答える
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が発生しますが、無効な値が何であるかがわかります。

于 2012-08-22T11:51:19.243 に答える
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
  }
}
于 2012-08-22T11:45:49.670 に答える
1

IndexOutOfBoundsExceptionjavaは実行時に配列の境界をチェックするので、注意を払わずにスローされると思います。この追加のチェックが必要だと確信していますか?

于 2012-08-22T11:46:04.770 に答える
1

'N'はクラスのメンバーに保存する必要があるようです。そうすれば、とにかくaMethod()メソッドにもアクセスできます。

いずれの場合も、コンストラクターパラメーターを必要とするメソッドをコンストラクターで呼び出すか、それらのコンストラクターパラメーターをメンバー変数に格納して他のメソッドで使用できるようにする必要があります。

于 2012-08-22T11:44:10.513 に答える
1

nSizeと同じように、クラスに新しいフィールドを追加します

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;            
 }
于 2012-08-22T11:50:00.887 に答える