【问题标题】:Recursive method not reaching the base case, however, I'm not sure why. Java递归方法没有达到基本情况,但是,我不确定为什么。爪哇
【发布时间】:2018-02-14 03:55:31
【问题描述】:

这是一个奇怪的项目,所以只用了非常奇怪的代码。我遇到的真正问题是为什么这种递归方法永远不会达到基本情况。

public int determinant() {
    int determinant = 0;
    if(getSize() == 2)
    {
        return (getElement(1, 1) * getElement (2, 2)) - (getElement(1,2) * getElement(2,1));
    }

    determinant += Math.pow(-1, getSize()+1) * getElement(getSize(), 1) * minor(getSize(),1).determinant();
    return determinant;
}

public SparseInterface minor(int row, int col) {
    SparseMatrix minor = new SparseMatrix();
    minor.matrix = matrix;
    for(int i = 0; i < matrix.size(); i++)
    {
        if(row == minor.matrix.get(i).getRow() || col == minor.matrix.get(i).getCol())
        {
            minor.matrix.remove(i);
        }
    }
    minor.size--;
    return minor;
}

因此,行列式调用minor,它返回一个新的方阵,其大小小1。我从一个 5x5 矩阵开始,并在其上调用行列式方法。然后,该方法采用 5x5 矩阵(4x4 矩阵)的次要行列式。这一直持续到我们到达基本情况,它是一个 2x2 矩阵并且计算行列式是微不足道的。

但是,问题在于代码永远不会超过它是 4x4 矩阵的点。每次调用minor时(它应该在每次递归调用中)它都应该输出更小的矩阵,因为minor中的最后一行代码minor.size--;。

通过一些错误测试,矩阵的大小似乎第一次减小了,但在随后的递归调用中没有。我对Java有点陌生,Java是否有一些关于方法调用中的方法调用的奇怪规则,导致它没有按应有的方式递减?除此之外,我不知所措。

提前致谢。

对于那些询问的人:

public int getElement(int row, int col) {
    int element = 0;
    if(row > getSize() || col > getSize())
    {
        System.out.println("Column/row combination is out of bounds.");
        return element;
    }

    for(int i = 0; i < matrix.size(); i++)
    {
        if(matrix.get(i).getRow() == row && matrix.get(i).getCol() == col)
        {
            element = matrix.get(i).getData();
        }
    }
    return element;
}

public int getSize() {
    return size;
}

Size 只是 SparseMatrix 类的一个数据成员,它表示我正在建模的奇怪的不存在矩阵是 SizexSize。有关更多详细信息,我在下面的 cmets 中对此进行了介绍。

public class SparseMatrix implements SparseInterface {
public LinkedList<Index> matrix;
public int size;

public SparseMatrix() {
    matrix = new LinkedList<Index>();
    size = 5;

}

【问题讨论】:

  • 请告诉我们getElement()getSize()的代码。
  • @RobbyCornelissen 次要不调用次要,但行列式调用行列式。
  • @AidanO'Connell 是的,你是对的。错过了。
  • 是的,据我所知,没有递归的事情发生。您还在每次递减之前在方法的开头更新 SparseMatrix。因此,您的 minor.size-- 每次都会做同样的事情,除非您以一种奇怪的构造函数方式将 SparseMatrix 创建为单例,我认为这是不可能的,或者 size 是 static final 之类的。我认为我们需要查看更多代码才能确切知道发生了什么,但此代码中没有发生递归。
  • 您的问题是您正在更新它,并且大小始终从 5 开始。它递减一次,然后调用您的方法,再次更新 SparseMatrix,再次将大小设置为 5,因为您刚刚更新了它,然后再次递减到 4。

标签: java recursion case base


【解决方案1】:

代码让我有点困惑。我认为您正试图在 minor() 方法中保持 SparseMatrix 对象的状态。

我认为你可能会寻找这样的东西?

minor.determinant() 有点令人困惑。 SparseMatrix 中有不同的determinant() 方法吗?您之前返回的是在次要方法中创建的 SparseMatrix 对象,并且之前有 minor(getSize(),1).determinant()。看起来您正在 SparseMatrix 类中调用 determinant() 方法,但我在代码中的该类中没有看到该方法。这些方法都在 SparseMatrix 类中吗?

我还建议将对象的变量名称更改为与您正在使用的方法名称不同的名称。它会降低代码的可读性。

无论如何,我认为这可以让你继续前进。

private SparseMatrix minor = new SparseMatrix();

public int determinant() {
    int determinant = 0;

    if(getSize() == 2)
    {
        return (getElement(1, 1) * getElement (2, 2)) - (getElement(1,2) * getElement(2,1));
    }

    minor(getSize(),1);

    determinant += Math.pow(-1, getSize()+1) * getElement(getSize(), 1) * this.minor.determinant();

    return determinant;
}

public void minor(int row, int col) {
    //in your previous code, I'm not sure where you're getting Matrix from, 
    //so I'll leave this in here - I think you're also wanting to use the 
    //matrix that is now stored in your minor object, so I changed that in
    //the for loop, but there are likely more elegant solutions for this if
    //I saw all the code
    minor.matrix = matrix;

    for(int i = 0; i < this.minor.matrix.size(); i++)
    {
        if(row == this.minor.matrix.get(i).getRow() || col == this.minor.matrix.get(i).getCol())
        {
            this.minor.matrix.remove(i);
        }
    }

    this.minor.size--;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-02-17
    • 2017-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-11
    • 1970-01-01
    • 2012-06-25
    相关资源
    最近更新 更多