日期:2014-05-20  浏览次数:20808 次

JAVA数组
package   com;

public   class   MatrixMultiply   {

/**
  *   @param   args
  */
public   static   void   main(String[]   args)   {
//   TODO   Auto-generated   method   stub
int   i,j,k;
int   a[][]={{1,3,-10},{5,-7,9}};
int   b[][]={{3,5,7,-5},{3,7,-2,6},{4,2,-5,-8}};
int   c[][]=new   int   [2][4];
for(i=0;i <2;i++){
for(j=0;i <4;j++){
c[i][j]=0;
for(k=0;k <3;k++)
c[i][j]+=a[i][k]*b[k][j];

}

}
System.out.println( "\nMatrix   A: ");
for(i=0;i <2;i++){
System.out.print( "[ ");
for(j=0;j <3;j++)
System.out.print(a[i][j]+ "   ");
System.out.println( "] ");


}
System.out.println( "\nMatrix   B: ");
for(i=0;i <3;i++){
System.out.print( "[ ");
for(j=0;j <4;j++)
System.out.print(b[i][j]+ "   ");
System.out.println( "] ");

}
System.out.println( "\nMatrix   C: ");
for(i=0;i <2;i++){
System.out.print( "[ ");
for(j=0;j <4;j++)
System.out.print(c[i][j]+ "   ");
System.out.println( "] ");
}
}


}

  错误:

    Exception   in   thread   "main "   java.lang.ArrayIndexOutOfBoundsException:   4
at   com.MatrixMultiply.main(MatrixMultiply.java:16)



------解决方案--------------------
for(j=0;i <4;j++){ 应该是for(j=0;j <4;j++){ 吧
------解决方案--------------------
我顶你个肺,这种错误都没看见 哈哈....第二个for循环休内错了