Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8636087
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T10:08:20+00:00 2026-06-12T10:08:20+00:00

This could be related to a problem with eclipse, but it is probably an

  • 0

This could be related to a problem with eclipse, but it is probably an issue with my code somewhere. Anyway, I have a MyLinkedList class and I use an iterator with it. When I was coding the iterator, it wasn’t letting me use the add method by using MyLinkedList.list.add(x, index). It tells me: “The method add(T, int) in the type MyLinkedList is not applicable for the arguments (T, int)”. The proposed fixes don’t change anything of the code at all(like wanting to change the parameter types of the method from (T, int) to (T, int). The remove method calls the same types of parameters and works fine. I’ll post the code to both the add method in MyLinkedList and the add method in the iterator.

The class:

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++;
}

The iterator method:

public void add(T x) 
    {
        if(modCount != expModCount)
            throw new ConcurrentModificationException();

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

        modCount--;
        index++;
    }

The entire program(there are definitely some things that aren’t completely correct, but I’m more focused with the issue with the add method. If you find any other problems with the code and would like to point me in the right direction, that would help me as well):

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);
    }

}
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T10:08:22+00:00Added an answer on June 12, 2026 at 10:08 am

    Hey the problem is type E in your Iterator interface and type E MyLinkedList class are colliding. If you remove the type E of Iterator this issue won’t come. However to ensure the type safety , consider implementing it like it is implemented collection frameworks for example something like in AbstractList.java

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this Java input-related problem. I'm solving some cases in UVAToolkit, but there
This is related to Clang 3.1 and C++11 support status , but I could
This question is related to my ASP.NET MVC 2 development, but it could apply
I've read several SO about related code snippets and suggestions for this, but I
This question is related to Java Refuses To Start - Could Not Resrve Enough
I believe this to be related partially to short-circuiting logic, but I couldn't find
This could be a very basic Javascript I know but I just can't get
Eclipse is driving me nuts right now. It's probably something trivial but I just
I've been battling with this issue for a couple of days now. I have
I have an array/pointer related problem. I created an int array myArray of size

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.