Java 我在hashfunction代码中遇到StackOverflow错误,但我无法确定,有人能帮我修复它吗/

Java 我在hashfunction代码中遇到StackOverflow错误,但我无法确定,有人能帮我修复它吗/,java,stack-overflow,Java,Stack Overflow,我正在为我的大学作业创建一个散列函数。我的哈希函数是这样工作的。。。它将接受一个字符串作为输入,并将每个字符的ASCII值添加到名为sum的整数变量中。这是在名为hash_func的函数中完成的。然后,在名为MYHashfunc的函数中,我使用递归来减小sum的值,使其小于使用哈希函数存储数据的数组的大小。因为我使用分离链接方法来解决冲突,所以我使用了LinkedList数组。 但是在MYhashfunc内部调用函数hash_func时,我得到了一个堆栈溢出错误。代码如下:- package h

我正在为我的大学作业创建一个散列函数。我的哈希函数是这样工作的。。。它将接受一个字符串作为输入,并将每个字符的ASCII值添加到名为sum的整数变量中。这是在名为hash_func的函数中完成的。然后,在名为MYHashfunc的函数中,我使用递归来减小sum的值,使其小于使用哈希函数存储数据的数组的大小。因为我使用分离链接方法来解决冲突,所以我使用了LinkedList数组。 但是在MYhashfunc内部调用函数hash_func时,我得到了一个堆栈溢出错误。代码如下:-

package hashfunction;

import java.util.LinkedList;
import java.util.Scanner;

public class Hashfunction {

public static int MyhashFUNC(String str,int A){
    int X=0;
    int sum = hash_func(str);
    if(sum<A)
        return sum;
    else{
        X = X+sum%10;
        sum /= 10;
        return(MyhashFUNC(str, A));
    }
}

public static int hash_func(String str) {
    int sum = 0;
    int len = str.length();
    for (int i = 0; i < len; i++) {
        if (str.charAt(i) >= '0' && str.charAt(i) <= '9') {
            sum += (int) str.charAt(i);
        } else if (str.charAt(i) >= 'a' && str.charAt(i) <= 'z' || 
         str.charAt(i) >= 'A' && str.charAt(i) <= 'Z') {
            sum += (int) str.charAt(i);
        }
    }
   return sum;
}

public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int N;
    int z;
    N = sc.nextInt();
    String[] str_array = new String[N];
    LinkedList<String>[] l_list = new LinkedList[N];
    for (int i = 0; i < N; i++) {
        l_list[i] = new LinkedList<String>();
    }
    for (int i = 0; i < N; i++) {
        str_array[i] = sc.next();
    }
    for (int i = 0; i < N; i++) {
        z = MyhashFUNC(str_array[i],N);
        if(l_list[z].peek()!="-1"){
                l_list[z].set(z, str_array[i]);
        }
        else{
            l_list[z].add(str_array[i]);
        }
    }

    for (int i = 0; i < N; i++) {
        int size = l_list[i].size();
          for (int j = 0; j < size; j++) {
              System.out.println(l_list[i].get(j));
        }
    }
}
}
包哈希函数;
导入java.util.LinkedList;
导入java.util.Scanner;
公共类哈希函数{
公共静态int-MyhashFUNC(字符串str,int-A){
int X=0;
int sum=hash_func(str);
如果(sum='0'&&str.charAt(i)='a'&&str.charAt(i)='a'&&str.charAt(i)在方法中

public static int MyhashFUNC(String str,int A){
    int X=0;
    int sum = hash_func(str);
    if(sum<A)
        return sum;
    else{
        X = X+sum%10;
        sum /= 10;
        return(MyhashFUNC(str, A));  // Call again MyhashFUNC with same parameters
    }
}
公共静态int-MyhashFUNC(字符串str,int-A){
int X=0;
int sum=hash_func(str);

如果(sum=a
您输入
else
块,然后使用相同的参数再次调用相同的方法。这将生成
StackOverFlow

,问题是:查看函数的返回:

return(MyhashFUNC(str, A));
它一次又一次地调用自己,没有任何东西可以阻止它。你不断地向调用堆栈添加堆栈帧,直到你得到-等待它-堆栈溢出

这是无停止条件递归的特点。

问题是, 这是递归函数,所以在每次递归调用中,输入参数都应该更改/不同/更新

public static int MyhashFUNC(String str,int A){
    int X=0;
    int sum = hash_func(str);
    if(sum<A)
        return sum;
    else{
        X = X+sum%10;
        sum /= 10;
        return(MyhashFUNC(str, A));//you are not updating any value and calling same function recursively. this will cause StackOverflowError.
    }
}
公共静态int-MyhashFUNC(字符串str,int-A){
int X=0;
int sum=hash_func(str);

如果两个输入都没有被修改过,为什么它会停止递归?你能详细解释一下吗?汉克斯,我犯了一个愚蠢的错误。我没注意到“sum”的值保持不变