[英]multi dimensional array debugging outofboundsexception
I am trying to make two 3 by 3 arrays (with predetermeined indexes) that will add themselves and display the output but I keep on getting an OOBE (out of bounds exception) error 我正在尝试制作两个3 x 3数组(具有预定索引),这些数组将自己添加并显示输出,但是我不断收到OOBE(越界异常)错误
// 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
}
Anywhere where you are doing col <= L1
or col <= l
needs changed to col < L1
and col < l
您在做
col <= L1
或col <= l
任何地方都需要更改为col < L1
和col < l
The max index of an array is always the length minus one because an array index starts at zero. 数组的最大索引始终是长度减去一,因为数组索引从零开始。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.