gpt4 book ai didi

java - 迭代器方法?

转载 作者:太空宇宙 更新时间:2023-11-04 07:20:21 26 4
gpt4 key购买 nike

我正在实现一个带有链接的列表接口(interface),但由于“ListADT”实现了 Iterable 接口(interface)。所以,我必须有一个生成迭代器的方法,我不知道该怎么做。我尝试像现在一样使用它,当我为链表创建一个对象,然后调用 iterator() 方法时,我得到了溢出。我知道该方法应该生成一个 Iterator 对象,但不确定如何生成。

import java.util.Iterator;

public class LinkedList<T> implements ListADT<T>
{
protected int count;
protected LinearNode <T> head, tail;
private int modCount;

public LinkedList()
{
count =0;
head = tail= null;
}

public T removeFirst()
{
T result = head.getElement();
head = head.getNext();
count--;
return result;

}

public T removeLast()
{
// THROW EMPTY EXCEPTION

T result;
LinearNode <T> previous = null;
LinearNode <T> current = head;
while(!current.equals(tail))
{
previous = current;
current = current.getNext();
}
result = tail.getElement();
tail = previous;
tail.setNext(null);
count--;
return result;

}

public T remove(T element)
{
// throw exception

boolean found = false;
LinearNode <T> previous = null;
LinearNode <T> current = head;

while (current != null && !found)
{
if(element.equals(current.getElement()))
found = true;
else
{
previous = current;
current = current.getNext();
}

if (!found)
{

}
else if (current.equals(head))
{
head = current.getNext();
}
else if(current.equals(tail))
{
tail = previous;
tail.setNext(null);
}
else
previous.setNext(current.getNext());
}
count --;
return current.getElement();
}

public T first()
{
return head.getElement();
}

public T last()
{
return tail.getElement();
}

public boolean contains(T target)
{
boolean found = false;
LinearNode <T> previous = null;
LinearNode <T> current = head;

while (current != null && !found)
{
if(target.equals(current.getElement()))
found = true;
else
{
previous = current;
current = current.getNext();
}
}
return found;
}

public boolean isEmpty()
{
boolean result = false;
if( head == null && tail ==null)
{
result = true;
}
return result;
}

public int size()
{
return count;
}

public Iterator<T> iterator()
{

return this.iterator();
}

public String toString()
{
LinearNode <T> current = head;
String result ="";
String line = "";
int loopCount=0;
while(current != null)
{
loopCount++;
line = loopCount + "> " + (String) current.getElement() + "\n";
result = result + line;
current = current.getNext();
}
return result;
}
}

最佳答案

你的问题

由于 this.iterator() 行,您会发生溢出在你的函数中public Iterator<T> iterator() ,来电,你猜对了public Iterator<T> iterator() .

<小时/>

方法 1:偷懒方式

如果您不打算在此类中使用迭代器(这看起来像是一个编程作业),您始终可以 super super 懒惰。

public Iterator<T> iterator() {
throw new UnsupportedOperationException("Pffffft you don't need no iterator");
}

此处列出此方法只是为了完整性。鉴于您的链接列表没有其他方法可以访问中间的随机元素而不删除其前面或后面的所有内容,我建议您:

不要这样做

<小时/>

方法 2:正确的方法

关于迭代器的事情是,它们执行列表功能的特定子集,即 hasNext() , next() ,和remove() 。如果你不确定这三种方法的作用,我建议你看看http://docs.oracle.com/javase/7/docs/api/java/util/Iterator.html

您应该创建一个公共(public)内部类。

public class LinkedList<T> implements ListADT<T> {
... stuff

private class MyIterator<T> implements Iterator<T> {
//It's best practice to explicitly store the head in the iterator
private LinearNode<T> head;

public MyIterator<T>(LinkedList<T>) {
...
}

@Override
public boolean hasNext() {
...
}

@Override
public T next() {
...
}

@Override
public void remove() {
...
}
}

public Iterator<T> iterator() {
return new MyIterator<T>(this);
}
}

现在,如果您真的很聪明,您可以根据迭代器重写其余代码。注:

这样做

关于java - 迭代器方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19430493/

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