For the assignment we're suppose to add a findFirst, findNext, and addElement method to the given class and test them. I manage to get the findNext and addElement working but not the findFirst.

Here's what the findFirst suppose to do
"Method findFirst(T item) searches for and returns the index of the element closest to the head node or returns -1 (not found)."

Here's my code

public int findFirst(T elem)
        int retval = -1;//return a -1 if not found
        Node tmp = head;

            for (int k = 0; k < size; k++)
                if (
                    return retval = k;//return index closest to head
                tmp = tmp.getNext();//going from head to tail
        return retval;

No matter what I test, it would always return -1.


Try debugging the code by adding some println statements that show where the code is executing and the values of the variables as they change and are used. The output will show what the computer is seeing and what the code is doing.

You are not using the parameter T for anything.

Thanks for the input guys. Would that mean my findNext method is wrong too ? I did not use the parameter T for anything in findNext also. It is some what similar to findFirst, but it works.

What is the parameter supposed to be used for?