【发布时间】:2020-03-29 14:17:45
【问题描述】:
大家好,我正在尝试实现 ListNode,但我对这两种方法都有问题
contains(E data)接受一个通用对象并检查其是否存在于列表中。countIf(E data)接受一个通用对象并检查它在 ListNode 中的出现。
我知道它可以以迭代的方式实现,但我想练习递归,所以请告诉我我做错了什么:
public class ListNode2<E> {
public E data;
public ListNode2<E> next;
public ListNode2(){
this.data=null;
this.next=null;
}
public ListNode2(E data, ListNode2<E> n) {
this.data = data;
next= n;
}
public ListNode2(E data) {
this.data = data;
next=null;
}
public E getData() {
return this.data;
}
public ListNode2<E> getNext() {
return this.next;
}
public ListNode2<E> addFirst(E data) {
return new ListNode2<>(data, this);
}
public boolean contains(E data){
if ( data ==null && this.getData()==null )
return true;
if (data!=null && (data.equals(this.getData())))
return true;
if (this.next==null)//remove the next and we have nullpointer exception but right results :(
return false;
return this.getNext().contains(data);
}
public int countIf(E data) {
//i was missing the getNext() but now i get for the first element zero
if(this.getNext()==null)
return 0;
if (data ==null && this.getData() == null )
return 1+ getNext().countIf(data);
if ( data!= null && (data.equals(this.getData())) )
return 1 + getNext().countIf(data);
return getNext().countIf(data);
}
public int size() {
if (getNext() == null) {
return 1;
} else {
return 1 + getNext().size();
}
}
@Override
public String toString() {
return data + " ";
}
public static void main(String[] args) {
ListNode2<Integer> list = null; // Leere Liste.
list = new ListNode2<Integer>(1); // ergibt 1.
list = list.addFirst(2);
list = list.addFirst(3);
list = list.addFirst(4);
int size = list.size();
System.out.println(size);
System.out.println("countif 1 "+ list.countIf(1));//this gives 0 why??
System.out.println("countif 2 "+ list.countIf(2));
System.out.println("countif 3 "+ list.countIf(3));
System.out.println("countif 5 "+ list.countIf(5));
System.out.println("countif null "+ list.countIf(null));
System.out.println("countif 7 "+ list.countIf(7));
}
}
我在 countIf 方法中做错了什么?我好像搞不懂?
编辑:
更正了用户建议的包含方法:Ole V.V.。
但在 countIF() 上应用相同的提示:它看起来像这样
public int countIf(E data) {
if (data!= null && (data.equals(this.getData())) )
return 1 + getNext().countIf(data);//this line is the 74
if ( data ==null && this.getData() == null )
return 1+ getNext().countIf(data);
if(this.getNext()==null)
return 0;
return getNext().countIf(data); //this line is the 83
}
输出:
Exception in thread "main" java.lang.NullPointerException
at de.hsmannheim.inf.pr2.ads.ListNode2.countIf(ListNode2.java:74)
at de.hsmannheim.inf.pr2.ads.ListNode2.countIf(ListNode2.java:83)
at de.hsmannheim.inf.pr2.ads.ListNode2.countIf(ListNode2.java:83)
at de.hsmannheim.inf.pr2.ads.ListNode2.countIf(ListNode2.java:83)
at de.hsmannheim.inf.pr2.ads.ListNode2.main(ListNode2.java:126)
/home/pi/.cache/netbeans/11.0/executor-snippets/run.xml:111: The following error occurred while executing this line:
/home/pi/.cache/netbeans/11.0/executor-snippets/run.xml:68: Java returned: 1
BUILD FAILED (total time: 0 seconds)
【问题讨论】:
标签: java recursion linked-list implementation