2d对象数组阵列

问题描述:

我想制作一个数组的二维数组,每个数组都填充另一个对象。我至今是:2d对象数组阵列

class CustomCache{ 
    boolean dirty = false; 
    int age = 0; 
    String addr; 
    public CustomCache(boolean a, String b, int c){ 
    dirty = a; 
     addr = b; 
     age = c; 
    } 

} 

class Setup { 
    int wpb; 
    CustomCache[] wpbArray = new CustomCache[wpb]; 
    public Setup(int a){ 
     wpb = a; 
    } 
} 

Setup[][] array = new Setup[numSets][numBlocks]; 
for(int i=0; i<numSets; i++){ 
     for(int j=0; j<numBlocks; j++){ 
      array[i][j] = new Setup(wpb); 
      for(int k=0; k<wpb; k++){ 
       array[i][j].wpbArray[k] = new CustomCache(false, "", 0); 
      } 
     }//end inner for 
    }//end outer loop 

我不断收到一个

java.lang.ArrayIndexOutOfBoundsException: 0 

这意味着数组为空。任何想法如何解决它?

这就是问题所在:

class Setup { 
    int wpb; 
    CustomCache[] wpbArray = new CustomCache[wpb]; 
    public Setup(int a){ 
     wpb = a; 
    } 
} 

这行:构造的

CustomCache[] wpbArray = new CustomCache[wpb]; 

运行之前身体 - 而wpb仍然是0。你想:

class Setup { 
    int wpb; 
    CustomCache[] wpbArray; 

    public Setup(int a) { 
     wpb = a; 
     wpbArray = new CustomCache[wpb]; 
    } 
} 

(我也建议改为更有意义的n但是这是另一回事。)

+0

你真了不起... – Failsafe 2013-05-01 19:24:46

+0

从来没有把你的答案标记为正确的答案。对于那个很抱歉。 – Failsafe 2014-08-01 12:12:15