请教一个有关矩阵的问题

叛逆的鲁鲁猫 2010-03-21 04:37:38
本人初学Java,写了一个矩阵相乘的程序,可是编译器提示数组越界,希望各位能够指出我的问题所在,谢了!
这是我写的程序:
import javax.swing.JOptionPane;

public class Matrix{
public static void main(String[] args){
int[][] MatrixOne=new int[3][2];
int[][] MatrixTwo=new int[2][1];
int[][] MatrixThree=new int[MatrixOne.length][MatrixTwo[0].length];
int i,j;
JOptionPane.showMessageDialog(null,"input the values of the array one:");
for(i=0;i<MatrixOne.length;i++)
{
for(j=0;j<MatrixOne[i].length;j++)
{
String inputOneString=JOptionPane.showInputDialog("Enter the number:");
MatrixOne[i][j]=Integer.parseInt(inputOneString);
}
}
JOptionPane.showMessageDialog(null,"input the values of the array two:");
for(i=0;i<MatrixTwo.length;i++)
{
for(j=0;j<MatrixTwo[i].length;j++)
{
String inputTwoString=JOptionPane.showInputDialog("Enter the number:");
MatrixTwo[i][j]=Integer.parseInt(inputTwoString);
}
}
MatrixThree=multiplyMatrix(MatrixOne,MatrixTwo);
String output="The nultiply matrix is";
for(i=0;i<MatrixOne.length;i++)
{
for(j=0;j<MatrixTwo[i].length;j++)
{
if((i+1)%MatrixOne.length==0)
output+=MatrixThree[i][j]+"\n";
else
output+=MatrixThree[i][j]+" ";
}
}
JOptionPane.showMessageDialog(null,output);
}
public static int[][] multiplyMatrix(int[][] a,int[][] b){
int[][] c=new int[a.length][b[0].length];
int i,j,k;
for(i=0;i<a.length;i++)
{
for(j=0;j<b[i].length;j++)
{
for(k=0;k<a[k].length;k++)
{
c[i][j]=a[i][k]*b[k][j];
}
}
}
return c;
}
}
...全文
106 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
Dazzlingwinter 2010-03-24
  • 打赏
  • 举报
回复

public class Matrix {
//Line And Row Number: line-a, row-b
private int a,b;

//Private 2-Dimension Integer Matrix
private int[][] mMatrix;

/**
* Constructor
*
* @param a lineNum
*
* @param b rowNum
*
* @param matrixValue MatrixValue
*/
public Matrix(int a, int b, int[] matrixValue) {
this.a = a;
this.b = b;
this.mMatrix = new int[a][b];
int valueIndex = 0;
for(int i=0; i<a; i++) {
for(int j=0; j<b; j++) {
mMatrix[i][j] = valueIndex < matrixValue.length ? matrixValue[valueIndex++] : 0;
}
}
}

public int[][] getMMatrix() {
return mMatrix;
}

//Plus With Another Matrix
public void martixPlus(Matrix matrix) {
if(b == matrix.a) {
int[][] plusResult = new int[a][matrix.b];
int[][] pMatrix = matrix.getMMatrix();
for(int i=0; i<a; i++) {
for(int j=0; j<matrix.b; j++) {
for(int k=0; k<b; k++) {
plusResult[i][j] += mMatrix[i][k] * pMatrix[k][j];
}
}
}
printPlusResult(mMatrix, pMatrix, plusResult);
}else {
System.out.println("Can Not Execute MatrixPlus...");
}
}

private void printPlusResult(int[][] mMatrix, int[][] pMatrix, int[][] plusResult) {
System.out.println("MatrixPlusResult:");
int lineIndex = mMatrix.length > mMatrix[0].length ? mMatrix.length : mMatrix[0].length;
for(int i=0; i < lineIndex; i++) {
System.out.print("|");
if(i < mMatrix.length) {
for(int j=0; j<mMatrix[i].length; j++) {
System.out.print(mMatrix[i][j] + "\t");
}
}else {
for(int j=0; j<mMatrix[0].length; j++) {
System.out.print(" \t");
}
}

if(Math.round(lineIndex/2) == i) {
System.out.print("|\t*\t|");
}else {
System.out.print("|\t \t|");
}


if(i < pMatrix.length) {
for(int j=0; j<pMatrix[i].length; j++) {
System.out.print(pMatrix[i][j] + "\t");
}
}else {
for(int j=0; j<pMatrix[0].length; j++) {
System.out.print(" \t");
}
}

if(Math.round(lineIndex/2) == i) {
System.out.print("|\t=\t|");
}else {
System.out.print("|\t \t|");
}

if(i < plusResult.length) {
for(int j=0; j<plusResult[i].length; j++) {
System.out.print(plusResult[i][j] + "\t");
}
}
System.out.println("|");
}
}


public static void main(String[] args) {
int[] a = new int[]{1,2,3,4,5,6};
int[] b = new int[]{1,2,3,4,5,6,7,8,9,10};

Matrix m1 = new Matrix(3,2, a);
Matrix m2 = new Matrix(2,5, b);

m1.martixPlus(m2);
}
}
/*输出控制有点儿小问题,自己解决一下吧~
MatrixPlusResult:
|1 2 | |1 2 3 4 5 | |13 16 19 22 25 |
|3 4 | * |6 7 8 9 10 | = |27 34 41 48 55 |
|5 6 | | | |41 52 63 74 85 |

*/
叛逆的鲁鲁猫 2010-03-24
  • 打赏
  • 举报
回复
谢谢各位,学到了不少东西。
继续努力!
叛逆的鲁鲁猫 2010-03-23
  • 打赏
  • 举报
回复
谢谢各位,大家看我代码看的辛苦了。
不过,哪位朋友可以写出一个比较简单的代码来,在下不胜感激。
「已注销」 2010-03-22
  • 打赏
  • 举报
回复
for(i=0;i<MatrixTwo.length;i++)
{
for(j=0;j<MatrixTwo[i].length;j++)
{
String inputTwoString=JOptionPane.showInputDialog("Enter the number:");
MatrixTwo[i][j]=Integer.parseInt(inputTwoString);
}
}
这里的问题!i可以是0、1、2而MatrixTwo只有[0][1]!
「已注销」 2010-03-22
  • 打赏
  • 举报
回复
for(i=0;i<MatrixTwo.length;i++)
{
for(j=0;j<MatrixTwo[i].length;j++)
{
String inputTwoString=JOptionPane.showInputDialog("Enter the number:");
MatrixTwo[i][j]=Integer.parseInt(inputTwoString);
}
}
这里的问题!
stu202060510 2010-03-21
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 wenzheng38 的回复:]
for(j=0;j<b[i].length;j++)//这个b[i]就有可能越界--->for(j=0;j<b[0].length;j++)
for(k=0;k<a[k].length;k++)//这个a[k]就有可能越界--->for(k=0;k<a[0].length;k++)
再试试看。。。
[/Quote]对楼主写的代码够用了,如果考虑扩展性的话,确实还有不少问题
wenzheng38 2010-03-21
  • 打赏
  • 举报
回复
for(j=0;j<b[i].length;j++)//这个b[i]就有可能越界--->for(j=0;j<b[0].length;j++)
for(k=0;k<a[k].length;k++)//这个a[k]就有可能越界--->for(k=0;k<a[0].length;k++)
再试试看。。。
stu202060510 2010-03-21
  • 打赏
  • 举报
回复
public class Matrix {
public static void main(String[] args) {
int[][] MatrixOne = new int[3][2];
int[][] MatrixTwo = new int[2][1];
System.out.println(MatrixOne.length);
System.out.println(MatrixTwo[0].length);
int[][] MatrixThree = new int[MatrixOne.length][MatrixTwo[0].length];

int i, j;
JOptionPane.showMessageDialog(null,
"input the values of the array one:");
for (i = 0; i < MatrixOne.length; i++) {
for (j = 0; j < MatrixOne[i].length; j++) {
String inputOneString = JOptionPane
.showInputDialog("Enter the number:");
MatrixOne[i][j] = Integer.parseInt(inputOneString);
}
}
JOptionPane.showMessageDialog(null,
"input the values of the array two:");
for (i = 0; i < MatrixTwo.length; i++) {
for (j = 0; j < MatrixTwo[i].length; j++) {
String inputTwoString = JOptionPane
.showInputDialog("Enter the number:");
MatrixTwo[i][j] = Integer.parseInt(inputTwoString);
}
}
MatrixThree = multiplyMatrix(MatrixOne, MatrixTwo);
String output = "The nultiply matrix is";
// for (i = 0; i < MatrixOne.length; i++) {
// for (j = 0; j < MatrixTwo[0].length; j++) {
// if ((i + 1) % MatrixOne.length == 0)
// output += MatrixThree[i][j] + "\n";
// else
// output += MatrixThree[i][j] + " ";
// }
// }
for(int a[]:MatrixThree){
for(int aa:a){
output+=aa+" ";
}
output+="\n";
}
JOptionPane.showMessageDialog(null, output);
}

public static int[][] multiplyMatrix(int[][] a, int[][] b) {
int[][] c = new int[a.length][b[0].length];
int i, j, k;
for (i = 0; i < a.length; i++) {
for (j = 0; j < b[0].length; j++) {
for (k = 0; k < a[0].length; k++) {
c[i][j] = c[i][j] + a[i][k] * b[k][j];
}
}
}
return c;
}
}

这代码看的真晕......
主要错误是:数组的下标越界
1)三重循环那有问题
i=2时 j<b[2].length(数组b的长度为2,只能调用b[1],所以会抛异常)
而且c[i][j] = c[i][j] + a[i][k] * b[k][j]而不是你写的c[i][j]=a[i][k] * b[k][j];这个你自己再理解一下
2)显示MatrixThree也有类似的问题,我帮你把显示方法修改了一下

62,624

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧