gpt4 book ai didi

java - 在 [] 的 ArrayList 中添加对象时出现堆栈溢出错误

转载 作者:行者123 更新时间:2023-12-01 15:00:33 25 4
gpt4 key购买 nike

当我尝试加载()时出现堆栈溢出错误。我正在尝试从 txt 文件读取 PhoneBookEntry 对象。 txt 文件具有构成 PhoneBookEntry 对象的名称和号码。

你能告诉我我做错了什么吗?

package HashSet;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.Scanner;

public class PhoneBook {
int capacity = 10;
private ArrayList<PhoneBookEntry>[] buckets;

public PhoneBook() {
this(10);
}

public PhoneBook(int size) {
capacity = size;
buckets = new ArrayList[size];
for (int i = 0; i < buckets.length; i++) {
buckets[i] = new ArrayList<PhoneBookEntry>();
}
}

public int getSize() {
int tot = 0;
for (ArrayList<PhoneBookEntry> x : buckets)
tot += x.size();
return tot;
}

public boolean add(PhoneBookEntry entry) {
if (contains(entry))
return false;
int x = Math.abs(entry.hashCode());
buckets[x % buckets.length].add(entry);
return true;
}

public void load()
{
InputStream is = getClass().getClassLoader().getResourceAsStream("phone.txt");
Scanner scan = new Scanner(is);
if (scan.hasNext())
add(new PhoneBookEntry(scan.next());
}
public void bucketSize() {
for (int i = 0; i < buckets.length; i++)
System.out.println(i + " " + buckets[i].size());
}

public boolean contains(PhoneBookEntry word) {
int x = Math.abs(word.hashCode());
return buckets[x % buckets.length].contains(word);
}

public int getCapacity() {
return capacity;
}

public static void main(String[] args) {
PhoneBook phone = new PhoneBook();
phone.load();
}
}
<小时/>
package HashSet;

import java.util.LinkedList;
import java.util.ListIterator;

public class PhoneBookEntry {
String n;
Integer nr;
LinkedList<PhoneBookEntry> list;

public PhoneBookEntry(String name, int number) {
list = new LinkedList<PhoneBookEntry>();
n = name;
nr = number;
list.add(new PhoneBookEntry(n, nr));
}

public String getN() {
return n;
}

public void setN(String n) {
this.n = n;
}

public Integer getNr() {
return nr;
}

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((n == null) ? 0 : n.hashCode());
result = prime * result + ((nr == null) ? 0 : nr.hashCode());
return result;
}

@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
PhoneBookEntry other = (PhoneBookEntry) obj;
if (n == null) {
if (other.n != null)
return false;
} else if (!n.equals(other.n))
return false;
if (nr == null) {
if (other.nr != null)
return false;
} else if (!nr.equals(other.nr))
return false;
return true;
}

public void setNr(Integer nr) {
this.nr = nr;
}

@Override
public String toString() {
return n + " " + nr;
}
}

最佳答案

每个新的电话簿条目都会创建一个新的电话簿条目,该条目又创建一个新的电话簿条目,该条目又创建一个新的电话簿条目,等等......无穷无尽。也就是说,直到堆栈空间用完。

您需要重新考虑您的应用程序数据结构。

关于java - 在 [] 的 ArrayList 中添加对象时出现堆栈溢出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13715035/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com