我在哈希函数代码中收到 StackOverflow 错误,但我无法确定,有人可以帮我修复它/
I am getting a StackOverflow error in a hashfunction code but I cannot determine , can someone help me fix it/
我正在为我的大学作业创建一个哈希函数。我的散列函数是这样工作的……它将一个字符串作为输入并将每个字符的 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));
}
}
}
}
方法中
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
}
}
如果 sum >= a
您进入 else
块并再次调用具有相同参数的相同方法。这将生成 Whosebug
.
这是问题所在:查看函数的 return:
return(MyhashFUNC(str, A));
它一次又一次地调用自己,没有任何东西可以阻止它。您不断向调用堆栈添加堆栈帧,直到出现 - 等待 - 堆栈溢出。
这是没有停止条件的递归的特点。
问题是,
这是递归函数,所以在每次递归调用时,你的输入参数应该是 change/different/updated.
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 WhosebugError.
}
}
我正在为我的大学作业创建一个哈希函数。我的散列函数是这样工作的……它将一个字符串作为输入并将每个字符的 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));
}
}
}
}
方法中
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
}
}
如果 sum >= a
您进入 else
块并再次调用具有相同参数的相同方法。这将生成 Whosebug
.
这是问题所在:查看函数的 return:
return(MyhashFUNC(str, A));
它一次又一次地调用自己,没有任何东西可以阻止它。您不断向调用堆栈添加堆栈帧,直到出现 - 等待 - 堆栈溢出。
这是没有停止条件的递归的特点。
问题是, 这是递归函数,所以在每次递归调用时,你的输入参数应该是 change/different/updated.
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 WhosebugError.
}
}