簡體   English   中英

2D Arrays 和矩陣 - Java

[英]2D Arrays & Matrices - Java

我不得不承認,我有點堅持這種 equals 方法。 我想我已經很接近了,因為我們獲得的第一個 JUnit 測試通過了,但是當我對一個應該返回 false 的測試進行測試時,我失敗了,它返回 true。 請問我可以在這方面得到一些幫助嗎?

public class Matrix {

// the dimensions of the matrix
private int numRows;
private int numColumns;

// the internal storage for the matrix elements 
private int data[][];

/**
 * @param d - the raw 2D array containing the initial values for the Matrix.
 */
public Matrix(int d[][])
{
    // d.length is the number of 1D arrays in the 2D array
    numRows = d.length; 
    if(numRows == 0)
        numColumns = 0;
    else
        numColumns = d[0].length; // d[0] is the first 1D array
    
    // create a new matrix to hold the data
    data = new int[numRows][numColumns]; 
    
    // copy the data over
    for(int i=0; i < numRows; i++) 
        for(int j=0; j < numColumns; j++)
            data[i][j] = d[i][j];
}



/**
 * Determines whether this Matrix is equal to another object.
 * @param o - the other object to compare to, which may not be a Matrix
 */
@Override // instruct the compiler that we intend for this method to override the superclass' (Object) version
public boolean equals(Object o)
{
    // make sure the Object we're comparing to is a Matrix
    if(!(o instanceof Matrix))
        return false;
    
    // if the above was not true, we know it's safe to treat 'o' as a Matrix
    Matrix m = (Matrix)o;
    
    /*
     * TODO: replace the below return statement with the correct code. 
     *  
     *  Returns true if this Matrix is equal to the input Matrix; returns false otherwise
     */ 
    boolean matches = true;
    
    if(o instanceof Matrix)
        for(int i = 0; i < data[i].length; i++)
            for(int j = 0; j < data[j].length; j++)
                if(o[i][j] != m[i][j])
                    matches = false;
    
    return matches;
}

問題在這里:

if(o instanceof Matrix)
    for(int i = 0; i < data[i].length; i++)
        for(int j = 0; i < data[j].length; j++)
            if(o[i][j] == m[i][j])
                return true;

您正在遍歷數組中的每個元素,當您找到與另一個數組中的元素匹配的任何元素時,您將返回true 我將創建一個boolean (最初設置為true ),當我發現一個不匹配的元素時,我會將boolean設置為false並在方法的末尾返回boolean

boolean matches = true;

if(o instanceof Matrix)
    for(int i = 0; i < data[i].length; i++)
        for(int j = 0; i < data[j].length; j++)
            if(o[i][j] != m[i][j])
                matches = false;

return matches;

這位作者問了幾乎相同的問題 為了簡化事情,我將在這里而不是那里回答。

如果我是老師,這些是我會一步一步指出的事情:

  1. 這不會編譯: if(o[i][j] != m[i][j]) om都不是數組,因此不能有下標。

  2. 假設我們做出這樣的改變: if (o.data[i][j].= m.data[i][j]) 現在o.data[i][j]不會編譯,因為o是一個Object類型,它沒有名為data的成員。

  3. 我們可以像這樣使用 class 演員表: if (((Matrix) o).data[i][j].= m.data[i][j]) 現在,它將編譯。 但是,上面的代碼有這一行: Matrix m = (Matrix)o; 所以, om指向同一個 object 只要是這樣, ((Matrix) o).data[i][j] == m.data[i][j]永遠是true

  4. 要解決上述問題,我們需要使用Matrix的兩個不同實例。 我們從參數中得到一個實例: o 第二個是隱含的,但有一個名字: this 所以,這就是我們想要的比較: if(this.data[i][j].= m.data[i][j])

以下是您問題的 scope 之外的其他一些評論:

一旦確定Matrix的兩個實例的內容不匹配,就可以使用break轉義for循環。 如果您正在處理大型矩陣,它可以節省時間。

@Override
public boolean equals (Object o) {
    if (o == this) return true;
    if (o == null) return false;
    if(!(o instanceof Matrix)) return false;
    

一般而言,從上述 3 個測試開始.equals方法是一個好主意。

     Matrix m = (Matrix)o;  
    

當覆蓋.equals時,強烈建議也覆蓋hascode () 因此,請考慮以下行:

    if (m.hashCode() != this.hashCode()) return false;

如果hashcode ()被正確實現,返回不同 hash 代碼的兩個對象保證為.equals返回false 但是,返回相同.equals代碼的兩個對象對於 .equals 可能返回truefalse 所以,如果hascode()測試返回 true,我們繼續測試:

// two matrices can have different sizes. Comparing the sizes early
// will not only save time, it  will guard against throwing 
// an `ArrayIndexOutOfBoundsException` when the sizes are different

if (    m.numRows    != this.numRows 
     || m.numColumns != this.numColumns) return false;

 boolean matches = true;

 outer: for(int i = 0; i < numRows; i++) {
           for(int j = 0; numColumns; j++) {
               if(this.data[i][j] != m.data[i][j]) {
                  matches = false;
                  if (!matches)
                    break outer;
            }
        }            
    }        
return matches;

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM