Java:在迭代器中使用MyLinkedList add方法

Java:在迭代器中使用MyLinkedList add方法,java,eclipse,iterator,linked-list,Java,Eclipse,Iterator,Linked List,这可能与eclipse的问题有关,但可能是我的代码的某个地方出了问题。无论如何,我有一个MyLinkedList类,并使用迭代器。当我编写迭代器时,它不允许我使用MyLinkedList.list.add(x,index)来使用add方法。它告诉我:“MyLinkedList类型中的add(T,int)方法不适用于参数(T,int)”。建议的修复根本不会更改任何代码(比如希望将方法的参数类型从(t,int)更改为(t,int)。remove方法调用相同类型的参数,并且工作正常。我将把代码发布到M

这可能与eclipse的问题有关,但可能是我的代码的某个地方出了问题。无论如何,我有一个MyLinkedList类,并使用迭代器。当我编写迭代器时,它不允许我使用MyLinkedList.list.add(x,index)来使用add方法。它告诉我:“MyLinkedList类型中的add(T,int)方法不适用于参数(T,int)”。建议的修复根本不会更改任何代码(比如希望将方法的参数类型从(t,int)更改为(t,int)。remove方法调用相同类型的参数,并且工作正常。我将把代码发布到MyLinkedList中的add方法和迭代器中的add方法

班级:

public void add(T x, int index)
{
    if((index < 0) || (index > size))
        throw new IndexOutOfBoundsException();
    if(size == 0)
    {
        this.head = this.tail = new Node(x, null, null);
    }
    else if(index == 0)
    {
        this.head = new Node(x, this.head, null);
        this.head = this.head.getNext().getPrev();
    }
    else if(index == size)
    {
        this.tail = new Node(x, null, this.tail);
    }
    else 
    {
        Node<T> temp = new Node(null, null, null);

        temp.setData(x);
        if(index < this.size() / 2)
        {
            temp = this.head;
            for(int i = 0; i < index; i++)
            {
                temp = temp.getNext();
            }
        }
        else
        {
            temp = this.tail;
            for(int i = this.size(); i > index; i--)
            {
                temp = temp.getPrev();
            }
        }

        temp.getNext().setPrev(temp);
        temp.getPrev().setNext(temp);
        temp.setData(x);

    }
    this.size++;
}
整个程序(肯定有一些事情不完全正确,但我更关注add方法的问题。如果您发现代码中存在任何其他问题,并希望为我指出正确的方向,这也会对我有所帮助):

公共类MyLinkedList扩展了AbstractList
{
私有静态类节点
{
私有T数据;
私有节点prev;
私有节点下一步;
/**
*建造师
*@param nodeData-存储在节点中的T类型数据
*@param nodePrev-初始化节点之前的节点
*@param nodeNext-初始化节点后的节点
*/
公共节点(T节点数据、节点节点EV、节点节点NEXT)
{
this.data=nodeData;
this.prev=nodePrev;
this.next=nodeNext;
}
/**
*获取此节点之前的节点
*@return返回此节点之前的节点
*/
公共节点getPrev()
{
返回此.prev;
}
/**
*将节点设置在此之前
*@param temp用于将上一个节点设置为
*/
公共void setPrev(节点温度)
{
this.prev=temp.prev;
}
/**
*获取此节点之后的节点
*@返回此节点后面的节点
*/
公共节点getNext()
{
把这个还给我,下一个;
}
/**
*设置列表中此节点之后的节点
*@param prev-节点
*/
公共void setNext(节点上一个)
{
this.next=prev.next;
}
/**
*从该节点获取数据
*@从该节点返回数据
*/
公共T getData()
{
返回此.data;
}
/**
*设置此节点中的数据
*@param data-要放入此节点的数据
*/
公共无效设置数据(T数据)
{
这个数据=数据;
}
}
私有整数大小;
私有int modCount;
公共节点头;
公共节点尾部;
/**
*MyLinkedList的构造函数,该构造函数将列表的开头和结尾设置为null,并将大小设置为0
*/
MyLinkedList()
{
this.head=新节点(null,null,null);
this.tail=新节点(null,null,null);
此值为0.size=0;
}
/**
*从索引指定的节点获取数据
*@param index-节点的索引
*@返回具有索引的节点的数据
*/
@凌驾
公共T获取(整数索引)
{
节点温度=新节点(null,null,null);
如果((索引<0)| |(索引>大小))
{
抛出新的IndexOutOfBoundsException();
}
if(索引索引;i--)
{
temp=temp.getPrev();
}
返回temp.getData();
}
}
/**
*将数据添加到指定索引处的列表中
*@param x-要存储的数据
*@param index-数据在列表中的存储位置
*/
公共无效添加(T x,整数索引)
{
如果((索引<0)| |(索引>大小))
抛出新的IndexOutOfBoundsException();
如果(大小==0)
{
this.head=this.tail=新节点(x,null,null);
}
else if(索引==0)
{
this.head=新节点(x,this.head,null);
this.head=this.head.getNext().getPrev();
}
else if(索引==大小)
{
this.tail=新节点(x,null,this.tail);
}
其他的
{
节点温度=新节点(null,null,null);
温度设置数据(x);
if(索引索引;i--)
{
temp=temp.getPrev();
}
}
temp.getNext().setPrev(temp);
temp.getPrev().setNext(temp);
温度设置数据(x);
}
这个.size++;
}
/**
*获取列表的大小
*@返回列表的大小
*/
@凌驾
公共整数大小()
{
返回此.size;
}
/**
*@return返回列表是否为空
*/
公共布尔值为空()
{
返回值(this.size()==0);
}
/**
*通过将列表的开头和结尾设置为null,并将大小设置为0来清除列表
*/
公共空间清除()
{
this.head=新节点(null,null,null);
this.tail=新节点(null,null,null);
this.tail=this.head.getNext();
此值为0.size=0;
}
/**
*从列表中删除节点
*@从移除的节点返回数据
*/
公共T删除(整型索引)
{
节点温度;
如果((索引<0)| |(索引>=大小)| | isEmpty())
{
抛出新的IndexOutOfBoundsException();
}
如果(索引==0)
{
temp=新节点(this.head.getData(),null,this.head.getNext());
this.head=this.head.getNext();
}
其他的
{
Node prev=新节点(null,null,null);
上一次=第n次(指数-1);
temp=新节点(prev.getNext().getData(),null,null);
}
这个尺寸--;
返回temp.getData();
}
/**
*获取索引处的节点
*@param index-中的
public void add(T x) 
    {
        if(modCount != expModCount)
            throw new ConcurrentModificationException();

        MyLinkedList.this.add(x, index); //the problem is in this line

        modCount--;
        index++;
    }
public class MyLinkedList<T> extends AbstractList<T> 
{

  private static class Node<T>
  {
    private T data;
    private Node<T> prev;
    private Node<T> next;

    /**
     * Constructor
     * @param nodeData - the data in type T that is stored in the node
     * @param nodePrev - the node previous to the node initialized
     * @param nodeNext - the node following the initialized node
     */
    public Node(T nodeData, Node<T> nodePrev, Node<T> nodeNext)
    {
        this.data = nodeData;
        this.prev = nodePrev;
        this.next = nodeNext;
    }

    /**
     * gets the node previous to this one
     * @return returns the node previous to this
     */
    public Node<T> getPrev()
    {
        return this.prev;
    }

    /**
     * sets the node previous to this
     * @param temp the node used to set the previous node to
     */
    public void setPrev(Node<T> temp)
    {
        this.prev = temp.prev;
    }

    /**
     * gets the node after this node
     * @return the node following this node
     */
    public Node<T> getNext()
    {
        return this.next;
    }

    /**
     * sets the node after this node in the list
     * @param prev - the node 
     */
    public void setNext(Node<T> prev)
    {
        this.next = prev.next;
    }

    /**
     * get the data from this node
     * @return the data from this node
     */
    public T getData()
    {
        return this.data;
    }

    /**
     * set the data in this node
     * @param data - the data to be put in this node
     */
    public void setData(T data)
    {
        this.data = data;
    }
}

private int size;
private int modCount;
public Node<T> head;
public Node<T> tail;

/**
 * Constructor for MyLinkedList that sets the head and tail of the list to point to null and the size set to 0
 */
MyLinkedList()
{
    this.head = new Node<T>(null, null, null);
    this.tail = new Node<T>(null, null, null);
    this.size = 0;
}


/**
 * gets the data from the node specified by the index
 * @param index - the index of the node
 * @return the data of the node with index index
 */
@Override
public T get(int index)
{
    Node<T> temp = new Node(null, null, null);

    if((index < 0) || (index > size))
    {
        throw new IndexOutOfBoundsException();
    }

    if(index < this.size() / 2)
    {
        temp = this.head;
        for(int i = 0; i < index; i++)
        {
            temp = temp.getNext();
        }

        return temp.getData();
    }
    else
    {
        temp = this.tail;
        for(int i = this.size(); i > index; i--)
        {
            temp = temp.getPrev();
        }

        return temp.getData();
    }
}

/**
 * adds data to the list at the specified index
 * @param x - the data to be stored
 * @param index - where in the list the data is stored
 */
public void add(T x, int index)
{
    if((index < 0) || (index > size))
        throw new IndexOutOfBoundsException();
    if(size == 0)
    {
        this.head = this.tail = new Node(x, null, null);
    }
    else if(index == 0)
    {
        this.head = new Node(x, this.head, null);
        this.head = this.head.getNext().getPrev();
    }
    else if(index == size)
    {
        this.tail = new Node(x, null, this.tail);
    }
    else 
    {
        Node<T> temp = new Node(null, null, null);

        temp.setData(x);
        if(index < this.size() / 2)
        {
            temp = this.head;
            for(int i = 0; i < index; i++)
            {
                temp = temp.getNext();
            }
        }
        else
        {
            temp = this.tail;
            for(int i = this.size(); i > index; i--)
            {
                temp = temp.getPrev();
            }
        }

        temp.getNext().setPrev(temp);
        temp.getPrev().setNext(temp);
        temp.setData(x);

    }
    this.size++;
}

/**
 * gets the size of the list
 * @return the size of the list
 */
@Override
public int size() 
{
    return this.size;
}

/**
 * @return returns whether or not the list is empty
 */
public boolean isEmpty()
{
    return (this.size() == 0);
}

/**
 * clears the list by setting the head and tail of the list equal to null and the size equal to 0
 */
public void clear()
{
    this.head = new Node<T>(null,null,null);
    this.tail = new Node<T>(null,null,null);

    this.tail = this.head.getNext();
    this.size = 0;
}

/**
 * removes a node from the list
 * @return the data from the removed node
 */
public T remove(int index)
{
    Node<T> temp;
    if ((index < 0) || (index >= size) || isEmpty())
    {
        throw new IndexOutOfBoundsException();
    }
    if(index == 0)
    {
        temp = new Node(this.head.getData(), null, this.head.getNext());
        this.head = this.head.getNext();
    }
    else
    {
        Node<T> prev = new Node(null,null,null);
        prev = getNth(index - 1);
        temp = new Node(prev.getNext().getData(), null, null);
    }
    this.size--;
    return temp.getData();
}

/**
 * gets the node at the index index
 * @param index - the index of the node we want to return
 * @return the node at the specified index
 */
private Node<T> getNth(int index)
{
    Node<T> temp;
    if(index < 0 || index > size)
        throw new IndexOutOfBoundsException();

    if(index < this.size() / 2)
    {
        temp = this.head;
        for(int i = 0; i < index; i++)
        {
            temp = temp.getNext();
        }
    }
    else
    {
        temp = this.tail;
        for(int i = this.size(); i > index; i--)
        {
            temp = temp.getPrev();
        }
    }
    return temp;
}

private class Iterator<T> implements ListIterator<T>
{
    private Node<T> currentNode;
    private int expModCount = modCount;
    int index = 0;

    /**
     * adds the data to the list using the add method from MyLinkedList
     */
    @Override
    public void add(T x) 
    {
        if(modCount != expModCount)
            throw new ConcurrentModificationException();

        MyLinkedList.this.add(x, index);

        modCount--;
        index++;
    }

    /**
     * @return returns true if the current node is not the tail, returns false if the current node is the tail of the list
     */
    @Override
    public boolean hasNext() 
    {
        if (currentNode.equals(MyLinkedList.this.tail))
            return false;
        else
            return true;
    }

    /**
     * @return returns true if the current node is not the head of the list, returns false if the current node is the head of the list
     */
    @Override
    public boolean hasPrevious()
    {
        if (currentNode.equals(MyLinkedList.this.head))
            return false;
        else
            return true;
    }

    /**
     * @return the data from the next node
     */
    @Override
    public T next() 
    {
        if(modCount != expModCount)
            throw new ConcurrentModificationException();
        if(hasNext() == false)
            throw new NoSuchElementException();

        T nextData = currentNode.getData();
        currentNode.setData(currentNode.getNext().getData());

        index++;

        return nextData;
    }

    /**
     * @return the index of the next node
     */
    @Override
    public int nextIndex()
    {
        return index + 1;
    }

    /**
     * @return the data from the previous node
     */
    @Override
    public T previous() 
    {
        if(modCount != expModCount)
        {
            throw new ConcurrentModificationException();
        }
        if(hasPrevious() == false)
        {
            throw new NoSuchElementException();
        }

        T prevData = currentNode.getPrev().getData();
        currentNode.setData(currentNode.getPrev().getData());

        index--;

        return prevData;
    }

    /**
     * @return the index of the previous node
     */
    @Override
    public int previousIndex() 
    {
        return index - 1;
    }

    /**
     * removes a node using the MyLinkedList remove method
     */
    @Override
    public void remove()
    {
        if(modCount != expModCount)
            throw new ConcurrentModificationException();

        MyLinkedList.this.remove(currentNode);
        modCount--;
    }

    /**
     * sets the data of the current node
     * @param x - the data to set to the current node.
     */
    @Override
    public void set(T x) 
    {
        currentNode.setData(x);
    }

}
}
private class MyIterator implements ListIterator<T>