在填充类(静态)数据结构/集合(在本例中为二维数组)的值时,通常最好将值直接保存到静态数组中,或者应该在方法中创建一个临时变量?在Java中创建类的数组
为了更清楚:
public class MyClass{
private static int[][] table;
public void setTable(int row, int col){
table = new char[row][col];
// fill table (eg. table[i][j] = 5;)
}
//// OR THIS//////
public void setTable(int row, int col){
int[][] tempTable = new tempTable[row][col];
// fill tempTable
table = tempTable;
}
}
创建一个临时变量感觉多余无用这里。但是有没有这种情况会被告知?通过执行方法和破坏数据中途程序崩溃的可能性?多线程应用程序?
我希望问题不是主观的,只是想知道是否存在固有的问题,直接填写。
如果静态对象可以/应该改变,那么第一种方法是好的。我更喜欢这是因为清晰度,但这是由于我的偏好,而不是任何技术优势。但是如果你想让它保持不可修改的状态,那么你应该使它成为static final int [] [] table = setTable(int,int)',并使用第二种方法。 – 2014-10-04 02:57:17