0

What is the correct way to make a constructor's argument accessible to different methods within a class?

For example, in the code snippet below, I want to make N accessible within a method called aMethod, without changing aMethod's existing argument signature. Is myArray.length the best alternative?

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 I'm testing for inputs greater than 0 so if a user enters 0 for i or 0 for j, the input is invalid.

4

7 回答 7

5

Just create a field for it, like you did for the array.

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

    ...
 }

However in this case I wouldn't do that, I'd change aMethod() instead:

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

(I also changed the check to allow [0..N-1] instead of [1..N], as arrays are indexed from 0.)

于 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

It seems 'N' should be stored in a member in your class. If you do that, then it's anyway accessible to the aMethod() method also.

In any case, you should either call that method that needs the constructor parameters, in the constructor, or store those constructor parameters in member variables and make them available to other methods.

于 2012-08-22T11:44:10.513 回答
1

我认为这IndexOutOfBoundsException会在你不注意的情况下被抛出,因为 java 在运行时检查数组边界。你确定你需要这个额外的检查吗?

于 2012-08-22T11:46:04.770 回答
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 回答