Java 陷入困境
我正试图用Java实现我自己的列表系统Java 陷入困境,java,list,loops,infinite,Java,List,Loops,Infinite,我正试图用Java实现我自己的列表系统 列表类文件: package RoutingDemo.List; /** * A 2-way Linked-List to store generic elements. * */ public class List { /* Instance Variables ---------------------------------------------------------------------------
列表
类文件:
package RoutingDemo.List;
/**
* A 2-way Linked-List to store generic elements.
*
*/
public class List {
/*
Instance Variables
---------------------------------------------------------------------------
*/
/**
* Reference to element.
*/
private Object info;
/**
* Reference to previous NodeList instance.
*/
private List prev;
/**
* Reference to next NodeList instance.
*/
private List next;
/*
Constructors
---------------------------------------------------------------------------
*/
/**
* Creates a new empty list.
*/
public List() {
prev = null;
next = null;
info = null;
}
/*
Methods
---------------------------------------------------------------------------
*/
/**
* Adds an element to the list.
*
* @param o Element to be added
*/
public List add(Object o) {
if(info == null) {
info = o;
prev = null;
next = null;
return this;
} else {
List temp = new List();
temp.add(o);
return addList(temp);
}
}
/**
* Appends an existing list to this list.
*
* @param newList List to be appended
*/
public List addList(List newList) {
if(newList.info() == null)
return this;
List ref = this;
ref.last().setNext(newList.first());
newList.first().setPrev(ref.last());
return ref;
}
/**
* Get number of elements in the list.
*
* @return number of elements in list
*/
public int count() {
if(info == null)
return 0;
List ref = this.first();
int count = 0;
while(true) {
count++;
if(!ref.isLast())
ref = ref.next();
else
break;
}
return count;
}
/**
* Deletes an element from the list.
*
* @param o Element to be deleted
* @return List which does NOT
* contain element o
*/
public List delete(Object o) {
if(info == null)
return this;
List ref = this.first();
while(true) {
if(ref.info() == o) {
if(ref.isFirst() && ref.isLast()) {
ref = new List();
break;
} else if(ref.isFirst()) {
ref = ref.next();
ref.killPrev();
break;
} else if(ref.isLast()) {
/* *** THIS IS THE CASE THAT WILL BE CALLED FOR THIS TEST **** */
ref = ref.prev();
ref.killNext();
break;
} else {
ref.prev().setNext(ref.next());
ref.next().setPrev(ref.prev());
ref = ref.prev();
break;
}
} else {
if(!ref.isLast())
ref = ref.next();
else
break;
}
}
return ref;
}
/**
* Moves to first element in List.
*
*
* @return List pointing to first
* element in list
*/
public List first() {
List ref = this;
while(!ref.isFirst()) {
/* *** STUCK HERE *** */
ref = ref.prev();
}
return ref;
}
/**
* Returns current list element.
*
* @return current list element
*/
public Object info() {
return info;
}
/**
* Checks whether list is empty.
*
* @return true, if list is empty
* , false otherwise.
*/
public boolean isEmpty() {
if(count() > 0)
return false;
else
return true;
}
/**
* Checks whether current element is the first element.
*
* @return true, if current element is
* first element, false otherwise.
*/
public boolean isFirst() {
if(prev == null)
return true;
else
return false;
}
/**
* checks whether current element is the last element.
*
* @return true, if current element is
* last element, false otherwise
*/
public boolean isLast() {
if(next == null)
return true;
else
return false;
}
/**
* Cuts the list from next element.
*
*
* @param l new link for current element
*/
public void killNext() {
next = null;
}
/**
* Cuts the list from previous element.
*
*
* @param l new link
*/
public void killPrev() {
prev = null;
}
/**
* Moves to last element in List.
*
*
* @return List pointing to last
* element in list
*/
public List last() {
List ref = this;
while(!ref.isLast()) {
ref = ref.next();
}
return ref;
}
/**
* Moves to next element in List
*
*
* @return List pointing to next
* element in list
*/
public List next() {
if(!isLast())
return next;
else
return this;
}
/**
* Moves to previous element in List
*
*
* @return List pointing to previous
* element in list
*/
public List prev() {
if(!isFirst())
return prev;
else
return this;
}
/**
* Sets the next link
*
*
* @param l new link for current element
*/
public void setNext(List l) {
next = l;
}
/**
* Sets the prev link for current element
*
*
* @param l new link
*/
public void setPrev(List l) {
prev = l;
}
}
我是这样测试的:
class Example {
Example() {
List nl = new List();
nl = nl.add(new Node(5,6));
System.out.println("" + nl.count());
Node x = new Node(1,3);
nl = nl.add(x);
System.out.println("" + nl.count());
nl = nl.delete(x);
System.out.println("as" + nl.count());
}
}
public class ListTest {
public static void main(String args[]) {
new Example();
}
}
ref.last().setNext(newList.first());
public List addList(List newList) {
if(newList.info() == null)
return this;
List ref = this;
List last = ref.last();
last.setNext(newList.first());
newList.first().setPrev(last);
return ref;
}
现在,当我添加前两个节点时,一切都很好。但是,在删除节点后调用count()
时,我会进入一个无限循环
在经历了很多断点之后,我在代码中标记了我被卡住的地方。显然,delete()
函数有问题,我无法找出我做错了什么
目前,我已将我的delete()
代码替换为:
public List delete(Object o) {
if(info == null)
return this;
List ref = this.first();
List temp = new List();
while(true) {
if(ref.info() != o)
temp.add(ref.info());
if(!ref.isLast())
ref = ref.next();
else
break;
}
return temp;
}
但这对于庞大的列表来说并不是记忆友好的。如果你能发现问题,请告诉我 可能是您在对象上使用==的事实
if(ref.info() == o)
即使这不是无限循环的确切问题,也是您需要解决的问题。您的代码包含无限循环的大量潜力。尝试重写如下所示的代码
while (true) {
// do something interesting
if (someCondition)
// go forward in the loop
else
break;
}
到
尽可能多
另外,请确保
列表最后一个元素的下一个永远不会指向列表的第一个元素,否则您将在很长一段时间内兜圈子。您的问题在addList中:
public List addList(List newList) {
if(newList.info() == null)
return this;
List ref = this;
//you should get a reference to the original "last"
ref.last().setNext(newList.first());
newList.first().setPrev(ref.last());
return ref;
}
改为这样做:
public List addList(List newList) {
if(newList.info() == null)
return this;
List ref = this;
List last = ref.last();
last.setNext(newList.first());
newList.first().setPrev(last);
return ref;
}
您总是将第二项添加到列表中,并使其成为自己的最后一项。这样,当您删除它时,它只会对自身执行killNext
操作,并且您的第一个节点不会被触及然后将自引用节点返回到调用示例,而不是您认为应该是剩余列表的引用。当您在该节点上调用count()
时,它将调用first()
,并将陷入循环中,因为!isFirst()将始终为true,并且它总是将自身引用为其上一个,因此它将在ref=ref.prev()中不断重新赋值代码>行。问题是您的列表最终已损坏。在列表中有两个项目时,它看起来如下所示:
class Example {
Example() {
List nl = new List();
nl = nl.add(new Node(5,6));
System.out.println("" + nl.count());
Node x = new Node(1,3);
nl = nl.add(x);
System.out.println("" + nl.count());
nl = nl.delete(x);
System.out.println("as" + nl.count());
}
}
public class ListTest {
public static void main(String args[]) {
new Example();
}
}
ref.last().setNext(newList.first());
public List addList(List newList) {
if(newList.info() == null)
return this;
List ref = this;
List last = ref.last();
last.setNext(newList.first());
newList.first().setPrev(last);
return ref;
}
列表{info=Node(5,6),prev=null,next=2}
列表{info=Node(1,3),prev=2,next=null}
呜呜,注意到列表的prev字段中的第二项指向它自己了吗?您的问题在于此方法:
public List addList(List newList) {
// ...
newList.first().setPrev(ref.last()); // <-- here
}
您要查找的是最后一项,就像您通过在末尾附加新列表来设置下一个字段之前一样。但是,通过再次调用last方法,您将在新列表追加后找到新的last项。这就是它的最后一个节点最终指向自身的原因
将addList方法更改为如下所示:
class Example {
Example() {
List nl = new List();
nl = nl.add(new Node(5,6));
System.out.println("" + nl.count());
Node x = new Node(1,3);
nl = nl.add(x);
System.out.println("" + nl.count());
nl = nl.delete(x);
System.out.println("as" + nl.count());
}
}
public class ListTest {
public static void main(String args[]) {
new Example();
}
}
ref.last().setNext(newList.first());
public List addList(List newList) {
if(newList.info() == null)
return this;
List ref = this;
List last = ref.last();
last.setNext(newList.first());
newList.first().setPrev(last);
return ref;
}
…它会起作用的。通过在修改列表之前缓存对列表末尾的引用,您现在拥有了正确的引用
即使如此,您的代码也比它必须的复杂得多。您应该查找一个如何实现双链接列表的示例,您会发现一些示例向您展示了如何实现双链接列表,这些示例要简单得多。你的删除方法特别复杂
我还认为您在将空列表表示为包含null的节点时存在问题。这似乎是导致你有各种各样的恶劣的边缘情况,你需要检查
我正试图用Java实现我自己的列表系统
我的建议是,不要。您应该重用内置列表,该列表是标准的且有效。如果你想知道它是如何工作的,你可以阅读源代码
知道如何编写自己的列表实现在面试中可能会很有用,但我强烈建议你永远不要在实际工作中这样做 很抱歉,你陷入了困境!要获得一些有用的见解,请查看对这个问题的回答:Don Branson。。。该链接链接到此页面。@Dykam,你是一个聪明的野兽。=)(请输入至少15个字符。):/我已进入空白地狱。有人想编辑这个并做4个空格缩进。但我的意思是通过引用来比较它。我将把List类扩展到NodeList等。我将在一个通过引用比较对象的环境中工作。无论如何,如果我必须根据对象所持有的信息来比较对象,我会让子类方法来做。我知道。我也讨厌使用循环格式。问题是>如果我给出一个条件:while(!listObject.isLast()){do this thing and increment},我还必须重写最后一个元素的代码。:)成功了!非常感谢。但我不太确定我是否听懂了你说的话。我的意思是,为什么我需要在修改它之前显式引用last()。你提出了与我完全相同的解决方案,但比我快。干得好,发现了它。很好。我想我没赢;)啊,是的。我现在看到了光明非常感谢:)它被标记为家庭作业,所以这可能是一个作业,而不是他可以使用内置类的东西。他至少可以阅读LinkedList的代码,因为这似乎是基于此。