# 请教一个有关矩阵的问题

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;
}
}
...全文
104 8 打赏 收藏 转发到动态 举报

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-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);
}
}

「已注销」 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],所以会抛异常）

2）显示MatrixThree也有类似的问题，我帮你把显示方法修改了一下

62,619

• 近7日
• 近30日
• 至今