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" java.lang.ArrayIndexOutOfBoundsException: 0
at test.Matrix.toString(Matrix.java:48)
at test.Matrix.main(Matrix.java:76)
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" java.lang.ArrayIndexOutOfBoundsException: 0
at test.Matrix.toString(Matrix.java:48)
at test.Matrix.main(Matrix.java:76)
数组越界这种情况出现的频率还是蛮多的.原因就是你访问的位置超过了数组的长度.
如你这里的s.append(" "+data[i][j]+" "); 这里的j应该指列,本来只有10列,但是
你却放到data[i][10]这样就错了.因为数组下标从0开始的.最多就能到9.这就是典型的数组越界了.
你安装这个思路,调试下你的程序
int rows = data.length;
int cols = data[0].length; //这里没有问题,为什么会出现越界?
答:毫无疑问,当int cols = data[0].length;出现越界时,一定是上一句:
int rows = data.length; 中rows值必定为0!(按现象推断出来的,请你用程序打印一下是不是)因此,请你检查一下:你的程序中data.length值为何会是0.
public void setData(int rows,int cols,double value){
this.rows = rows;
this.cols = cols;
data[rows][cols] = Math.random()*value;
}
改为
public void setData(int rows,int cols,double value){
data[rows][cols] = Math.random()*value;
}
就像1楼说的。看看程序在运行或者初始化的时候为什么data的lenght为0!
this.rows = rows;
this.cols = cols;
data[rows][cols] = Math.random()*value;
}
这样的话 rows 和 cols 都是 0 ,这是为什么?我觉得和下面的无惨构造函数没有关系啊public Matrix(){
this(0,0);
} 即便是我我改成这样
public Matrix(){
this(10,10);
}
最终rows ,cols的值还是0,不知道为什么?不理解这两个有什么区别吗?
public void setData(int rows,int cols,double value){
this.rows = rows;
this.cols = cols;
data[rows][cols] = Math.random()*value;
}
public void setData(int rows,int cols,double value){
data[rows][cols] = Math.random()*value;
}
for(int j=0;j <A.cols;j++)
A.setData(i, j, 10.0);
}
执行的第一次i=0,j=0时,你写的this.rows = rows;
this.cols = cols;
就把A.rows和A.cols都改成0了啊
而是改成System.out.println(A.multiply(B).rows);
System.out.println(A.multiply(B).cols);为什么打印出的结果,上一句是0
下一句是输入的rows-1,比如输入3,4,4,3,下面打出的是2;输入5,5,5,5,打出的是4;我感觉应该都是0才对啊
有人解释下么