2012-06-14 67 views
0

我想做兩個3×3數組(預定義索引),將自己添加並顯示輸出,但我一直得到一個OOBE(超出界限例外)錯誤有人可以請幫助我任何輸入大大appricated,三江源多維數組調試outofboundsexception

// The "Matrixsum" class. 
import java.awt.*; 
import hsa.Console; 

public class Matrixsum 
{ 
    static Console c;   // The output console 

    public static void main (String[] args) 
    { 
     c = new Console(); 


    int array[] [] = {{4, 5, 3}, {6, 8, 3}, {1, 2, 3}}; 
    int array1[] [] = {{5, 4, 3}, {5, 6, 3}{1, 2, 3}}; 

    c.println ("Number of Row= " + array.length); 
    c.println ("Number of Column= " + array [1].length); 

    int l = array.length; 

    c.println ("\t\t\nMatrix 1 :\n "); 
    for (int row = 0 ; row < l ; row++) 
    { 
     for (int col = 0 ; col <= l ; col++) 
     { 
      c.print (" " + array [row] [col]); 
     } 
     c.println(); 
    } 
    int L1 = array1.length; 
    c.println ("Matrix 2 : "); 
    for (int row = 0 ; row < L1 ; row++) 
    { 
     for (int col = 0 ; col <= L1 ; col++) 
     { 
      c.print (" " + array1 [row] [col]); 
     } 
     c.println(); 
    } 
    c.println ("Addition of both matrix : "); 
    for (int row = 0 ; row < L1 ; row++) 
    { 
     for (int col = 0 ; col <= L1 ; col++) 
     { 
      c.print (" " + (array [row] [col] + array1 [row] [col])); 
     } 
     c.println(); 
    } 


    // Place your program here. 'c' is the output console 
} // main method 

}

回答

3

任何地方你正在做col <= L1col <= l需要改變col < L1col < l

數組的最大索引始終是長度減1,因爲數組索引從零開始。

+0

正確。這也可以通過調試並查看與數組大小相比太高的值來找到。 –