Java OOP类Matrix方法等于

时间:2016-03-03 03:01:52

标签: java oop matrix methods equals

更新: 当我检查两个矩阵是否相等时,我的程序总是返回false。初始化和复制工作正常,我可以通过控制台验证打印后两个矩阵是否相同。无论我做什么,equals方法总是返回false!

// Class Matrix (Matrix.java)
import java.util.Scanner;
import java.util.Random;

public class Matrix {  
private int size;
private int[][] table = new int[MAX][MAX];


//Default constructor
public Matrix() {
size = 0;
}

//Alternate constructor
public Matrix(int s) {
size = s;
}

//Method to initiate a matrix with random values
  public void init(int low, int up) {
    Random rand = new Random(); 
    for (int r = 0; r < size; r++) {
      for (int c = 0; c < size; c++)
        table[r][c] = rand.nextInt(up - low + 1) + low;
    }
  }

//Method to copy the matrix
public void copy(Matrix a) {
  for (int r = 0; r < size; r++) {
    for (int c = 0; c < size; c++)
      table[r][c] = a.table[r][c];
  }
}

//Method to compare two matrices for equality
  public boolean equals(Object obj) {
    boolean result = false;
    if (obj instanceof Matrix) { 
      Matrix otherMatrix = (Matrix) obj;
      for (int r = 0; r < size; r++) {
        for (int c = 0; c < size; c++)
          //problem solved!
          //previous:
          //result = (table[r] == otherMatrix.table[r] && table[c] == otherMatrix.table[c]);
          //fixed
          result = (table[r][c] == otherMatrix.table[r][c]);
      }
    }
    return result;
  }

测试客户:

first.init(LOW, UP);
System.out.println("The original matrix is:");
first.print();
System.out.println("The copy of this matrix is: ");
result.copy(first);
result.print();
System.out.println("Testing for equality. Should be equal!!");
if (result.equals(first))
  System.out.println("The matrices are equal!!");
else
  System.out.println("The matrices are NOT equal!!");

3 个答案:

答案 0 :(得分:0)

public boolean equals(Object obj) {
  boolean match = false;
  if (obj instanceof Matrix) { 
    Matrix otherMatrix = (Matrix) obj;
    for (int r = 0; r < size; r++) {
      for (int c = 0; c < size; c++)
        match = (table[r] == otherMatrix.table[r] && table[c] == otherMatrix.table[c]);
    }
  }
  return match ;
}

答案 1 :(得分:0)

for周期可以运行0次,这将产生无返回语句。 你可以更新到这样的东西。

public boolean equals(Object obj) {
    boolean result = false;
    if (obj instanceof Matrix) { 
          Matrix otherMatrix = (Matrix) obj;
          // I copied your logic here, are you sure it is correct? -->
          for (int r = 0; r < size; r++) {
              for (int c = 0; c < size; c++)
                result = table[r] == otherMatrix.table[r] && table[c] == otherMatrix.table[c];
          }
          // <--
    }
    return result;
}

// testing part removed, because lack of context

答案 2 :(得分:0)

修正了我的计划,我真诚地感谢你的帮助:)

当我检查两个矩阵是否相等时,我的程序总是返回false。初始化和复制工作正常,我可以通过控制台验证打印后两个矩阵是否相同。无论我做什么,equals方法总是返回false!

// Class Matrix (Matrix.java)
import java.util.Scanner;
import java.util.Random;

public class Matrix {  
private int size;
private int[][] table = new int[MAX][MAX];


//Default constructor
public Matrix() {
size = 0;
}

//Alternate constructor
public Matrix(int s) {
size = s;
}

//Method to initiate a matrix with random values
  public void init(int low, int up) {
    Random rand = new Random(); 
    for (int r = 0; r < size; r++) {
      for (int c = 0; c < size; c++)
        table[r][c] = rand.nextInt(up - low + 1) + low;
    }
  }

//Method to copy the matrix
public void copy(Matrix a) {
  for (int r = 0; r < size; r++) {
    for (int c = 0; c < size; c++)
      table[r][c] = a.table[r][c];
  }
}

//Method to compare two matrices for equality
  public boolean equals(Object obj) {
    boolean result = false;
    if (obj instanceof Matrix) { 
      Matrix otherMatrix = (Matrix) obj;
      for (int r = 0; r < size; r++) {
        for (int c = 0; c < size; c++)
          //problem solved!
          //previous:
          //result = (table[r] == otherMatrix.table[r] && table[c] == otherMatrix.table[c]);
          //fixed
          result = (table[r][c] == otherMatrix.table[r][c]);
      }
    }
    return result;
  }

测试客户:

first.init(LOW, UP);
System.out.println("The original matrix is:");
first.print();
System.out.println("The copy of this matrix is: ");
result.copy(first);
result.print();
System.out.println("Testing for equality. Should be equal!!");
if (result.equals(first))
  System.out.println("The matrices are equal!!");
else
  System.out.println("The matrices are NOT equal!!");