gpt4 book ai didi

java - 在链接结构中删除

转载 作者:行者123 更新时间:2023-11-29 03:26:54 26 4
gpt4 key购买 nike

您好,我正在尝试测试 removeCity(),但它没有删除我提供的任何元素。还有方法 addToList() 如果我在 City 类中使​​用它,我会得到“线程“主”java.lang.StackOverflowError 中的异常”,而它在测试类中工作正常

有什么帮助吗?

我的 list

public class MyList<T> {
private Node head;
private Node tail;

public MyList(){
head = null;
tail = null;
}

public void addToTail(T info){
Node n;
//case 1: empty List
if(isEmpty()){
n = new Node(info, null);
head = n;
tail = head;
}
//case 2: if the list is not empty
else {
n = new Node(info, null);
tail.setNext(n);
tail = n;
}
}

public void addToHead(T info){
Node n;
//case 1: empty List
if(isEmpty()){
n = new Node(info, null);
head = n;
tail = head;
}
//case 2: if the list is not empty
else {
n = new Node(info, head);
head = n;
}
}

public boolean removeHead(){
//Case 1: if the list is empty
if(isEmpty())
return false;
//case 2: if the list have at least one element
else{
Node n = head.getNext();
head = n;
return true;
}

}

public boolean removeElement(String element){

//cacs 1 if before is the head
if(isEmpty())
return false;

if( ((City) head.getInfo()).getCode().equals(element)){
removeHead();
return true;
}

Node iter = head.getNext();
Node prev = head;
while(iter != null && !((City) head.getInfo()).getCode().equals(element)){
iter = iter.getNext();
prev = prev.getNext();
}
if(iter == null)
return false;
else{
prev.setNext(iter.getNext());
return true;
}
}

//To check if the list is empty
public boolean isEmpty(){
if ( head == null)
return true;
else
return false;
}

节点

public class Node<T> {


private T info;
private Node next;

public Node(){
info = null;
next = null;
}

public Node(T info, Node next){
this.info = info;
this.next = next;
}

public T getInfo(){
return info;
}

public Node getNext(){
return next;
}

public void setNext(Node next){
this.next = next;
}

public void setInfo(T info){
this.info = info;
}

}

城市

    public class City implements Serializable {
public static MyList<City> cityList = new MyList<City>();
private String name;
private String code;

public City(String name, String code) {
super();
this.name = name;
this.code = code;
addToList(new City(name,code));
}

public void addToList(City toAdd){
City.cityList.addToHead(toAdd);
}

public static void removeCity(String name){
if( cityList.isEmpty()){
System.out.println("The List is empty");
return;
}
if ( cityList.removeElement(name) == true )
System.out.println("The City was removed sucssesfully");
else
System.out.println("This city does not not exist");
}

}

测试

public class DummyTest {
public static void main(String [] args){
City x = new City("Ney York","NY");
City y = new City("London","LD");
System.out.println(City.cityList);
}
}

堆栈跟踪

Exception in thread "main" java.lang.StackOverflowError
at City.<init>(City.java:15)
at City.<init>(City.java:18)
at City.<init>(City.java:18)

第15行是构造函数

public City(String name, String code)

第18行是addToList

addToList(new City(name,code))

最佳答案

我发现您在 removeElement 方法的 while 循环中有问题。我不确定它是否能解决您的问题。如果你得到 StackOverflowException,你能不能也把堆栈跟踪的一部分放在这里。

    Node iter = head.getNext();
Node prev = head;
while(iter != null && !((City) head.getInfo()).getCode().equals(element)){
iter = iter.getNext();
prev = prev.getNext();
}

这一行

   while(iter != null && !((City) head.getInfo()).getCode().equals(element))

应该是吧

   while(iter != null && !((City) iter.getInfo()).getCode().equals(element))

iter 而不是 head

关于java - 在链接结构中删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20567291/

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