shawn.bug 2008-11-14 22:26
浏览 184
已采纳

为什么会有这样的异常?

import java.io.*;
public class KeyInput
{
public static int readInt() throws IOException
{
String s;
InputStreamReader ir;
BufferedReader in;
ir=new InputStreamReader(System.in);
in=new BufferedReader(ir);
System.out.print(": ");
s=in.readLine();
int i=Integer.parseInt(s);

    return i;
}
public static double readDouble() throws IOException
{
   String s;
    InputStreamReader ir;
    BufferedReader in;
    ir=new InputStreamReader(System.in);
    in=new BufferedReader(ir);
    s=in.readLine();
    double d=Double.parseDouble(s);
    return d;
}

}

import java.io.IOException;
public class Matrix{
private int rows;
private int cols;
double[][] data;
public Matrix(){
this(0,0);
}
public Matrix(int rows,int cols){
this.rows = rows;
this.cols = cols;
data = new double[rows][cols];
}
public Matrix(int rows,int cols,double data[][]){
this.rows = rows;
this.cols = cols;
this.data = data;
}
public void setData(int rows,int cols,double value){
this.rows = rows;
this.cols = cols;
data[rows][cols] = Math.random()*value;
}
public double getData(int rows,int cols){
return data[rows][cols];
}
public Matrix multiply(Matrix B){
Matrix A = this;
if(A.cols!=B.rows)
new IllegalArgumentException("矩阵不匹配!");
Matrix C = new Matrix(A.rows,B.cols);

    for(int i=0; i<C.rows; i++)
        for(int j=0; j<C.cols; j++)
            for(int k=0; k<A.cols; k++)
                C.data[i][j] += (A.data[i][k]*B.data[k][j]);
    return C;
}
public String toString(){
    StringBuffer s = new StringBuffer();
    int rows = data.length;
    int cols = data[0].length;
    s.append("Matrix ["+rows+"]["+cols+"] ="+"\n");
        for(int i=0; i<rows; i++){
            for(int j=0; j<cols; j++)
                s.append(" "+data[i][j]+" ");
                s.append("\n");
        }
        s.append("\n");
    return s.toString();
}
public static void main(String args[]) throws IOException{
    int rows,cols;
    rows = KeyInput.readInt();
    cols = KeyInput.readInt();

    Matrix A = new Matrix(rows,cols);
    for(int i=0;i<A.rows;i++){
        for(int j=0;j<A.cols;j++)
    A.setData(i, j, 10.0);
    }

    rows = KeyInput.readInt();
    cols = KeyInput.readInt();
    Matrix B = new Matrix(rows,cols);
    for(int i=0;i<rows;i++){
        for(int j=0;j<cols;j++)
    B.setData(i, j, 10.0);
    }
    System.out.println(A.multiply(B).toString());

}
}

Exception in thread "main" [color=red]java.lang.ArrayIndexOutOfBoundsException[/color]: 0
at test.Matrix.toString(Matrix.java:48)
at test.Matrix.main(Matrix.java:76)

为什么?

  • 写回答

2条回答 默认 最新

  • iteye_521 2008-11-14 22:41
    关注

    问题在toString方法中,设置一个断点单步跟踪debug一下吧

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)