C# 为什么在非托管错误中会出现堆栈溢出异常

C# 为什么在非托管错误中会出现堆栈溢出异常,c#,recursion,C#,Recursion,程序需要使用递归检查数组是否是回文的,但我在非托管中得到了堆栈溢出异常。卡在上面一天多了,请帮忙 public static void Main(string[] args) { char[] arr = { 'd', 'a', 'd' }; int ind = 0; Rowpalindrome(arr, ind); } static bool Rowpalindrome(char[] arr, int index) { if (index == arr.Le

程序需要使用递归检查数组是否是回文的,但我在非托管中得到了堆栈溢出异常。卡在上面一天多了,请帮忙

public static void Main(string[] args)
{
    char[] arr = { 'd', 'a', 'd' };
    int ind = 0;

    Rowpalindrome(arr, ind);
}

static bool Rowpalindrome(char[] arr, int index)
{
    if (index == arr.Length % 2)
        return true;

    int i = index;

    if (arr[i] != arr[(arr.Length - index - 1)])
        return false;
    else
        return Rowpalindrome(arr, index++);
}
您在增量中有错误;它应该是
++index
,而不是
index++

return Rowpalindrome(arr, ++index);
您应该递增并传递
索引的修改值(
++index
),而不是递增并传递初始值(
索引++
)。更好的实现方法是将其简化为:

return Rowpalindrome(arr, index + 1);
编辑:您也有一些逻辑错误(感谢Fildor指出):条件应该是

if (arr.Length <= 1 || index > arr.Length % 2 + 1)
    return true;
结果:

[]              : Palindrome
[a]             : Palindrome
[a, a]          : Palindrome
[a, b]          : Not
[a, b, a]       : Palindrome
[a, b, c]       : Not
[a, b, b, a]    : Palindrome
[a, b, c, a]    : Not
[a, b, b, c]    : Not
编辑2:如果是多维数组(见下面的注释),您可以将感兴趣的列提取到数组中并运行例程,例如对于
2D
数组:

char[,] c = new char[,] {
  { 'a', 'b', 'c'},
  { 'x', 'y', 'z'},
  { 'x', 'p', 'q'},
  { 'a', 'm', 'n'},
};

int colIndex = 0; // should be {'a', 'x', 'x', 'a'} column

// c.GetLength(0) - number of lines (length of the 1st dimension) - 4
char[] a = new char[c.GetLength(0)];

for (int i = 0; i < a.Length; ++i)
  a[i] = c[i, colIndex];

bool result = Rowpalindrome(a, 0);
char[,]c=新字符[,]{
{'a','b','c'},
{'x','y','z'},
{'x','p','q'},
{'a','m','n'},
};
int colIndex=0;//应该是{'a','x','x','a'}列
//c.GetLength(0)-行数(第一个尺寸的长度)-4
char[]a=新字符[c.GetLength(0)];
对于(int i=0;i
增量中有错误;它应该是
++index
,而不是
index++

return Rowpalindrome(arr, ++index);
您应该递增并传递
索引的修改值(
++index
),而不是递增并传递初始值(
索引++
)。更好的实现方法是将其简化为:

return Rowpalindrome(arr, index + 1);
编辑:您也有一些逻辑错误(感谢Fildor指出):条件应该是

if (arr.Length <= 1 || index > arr.Length % 2 + 1)
    return true;
结果:

[]              : Palindrome
[a]             : Palindrome
[a, a]          : Palindrome
[a, b]          : Not
[a, b, a]       : Palindrome
[a, b, c]       : Not
[a, b, b, a]    : Palindrome
[a, b, c, a]    : Not
[a, b, b, c]    : Not
编辑2:如果是多维数组(见下面的注释),您可以将感兴趣的列提取到数组中并运行例程,例如对于
2D
数组:

char[,] c = new char[,] {
  { 'a', 'b', 'c'},
  { 'x', 'y', 'z'},
  { 'x', 'p', 'q'},
  { 'a', 'm', 'n'},
};

int colIndex = 0; // should be {'a', 'x', 'x', 'a'} column

// c.GetLength(0) - number of lines (length of the 1st dimension) - 4
char[] a = new char[c.GetLength(0)];

for (int i = 0; i < a.Length; ++i)
  a[i] = c[i, colIndex];

bool result = Rowpalindrome(a, 0);
char[,]c=新字符[,]{
{'a','b','c'},
{'x','y','z'},
{'x','p','q'},
{'a','m','n'},
};
int colIndex=0;//应该是{'a','x','x','a'}列
//c.GetLength(0)-行数(第一个尺寸的长度)-4
char[]a=新字符[c.GetLength(0)];
对于(int i=0;i
您的帮助称为调试器;-)参见
返回行回文(arr,++索引)或使用
返回行回文(arr,索引+1)您的帮助称为调试器;-)参见
返回行回文(arr,++索引)或使用
返回行回文(arr,索引+1)如果(索引==arr.Length%2)
在我看来也不正确,则此
。这将使任何偶数长度的数组成为行回文,不是吗?(不是作为单独的答案添加,因为它不会导致SOE)@Fildor:你说得很对,谢谢!代码中有一个逻辑错误。非常感谢!你能帮我问另一个问题吗?我只需要做同样的事情,得到一个多维数组和一个列,我需要检查collum是否为palindrome@johnsmith这将是一个新问题。@john smith:您可以尝试将列读入数组,然后调用例程。对于2D数组:当给定
char[,]c=。。。int columnIndex=…
您可以执行
char[]a=char[c.GetLength(0)];对于(int r=0;r如果(索引==arr.Length%2)
在我看来也不正确,则此
。这将使任何偶数长度的数组成为行回文,不是吗?(不是作为单独的答案添加,因为它不会导致SOE)@Fildor:你说得很对,谢谢!代码中有一个逻辑错误。非常感谢!你能帮我问另一个问题吗?我只需要做同样的事情,得到一个多维数组和一个列,我需要检查collum是否为palindrome@johnsmith这将是一个新问题。@john smith:您可以尝试将列读入数组,然后调用例程。对于2D数组:当给定
char[,]c=。。。int columnIndex=…
您可以执行
char[]a=char[c.GetLength(0)];对于(int r=0;r