2012-08-22 76 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代表,你会希望如此。;) –

相关问题