1

I keep getting an error telling me that add(T) is undefined for the type T when trying to add add:

package p07;
import java.util.ArrayList;

public class MyList<T extends Number>
{
    private T ArrayList;

    public MyList(T ArrayList)
    {
        this.ArrayList=ArrayList;
    }
    public void add(T x)
    {
        ArrayList.add(x);
    }
}

Any advice on how to proceed?

1
  • You should use MyList().add() not T().add() Commented Oct 11, 2015 at 15:53

1 Answer 1

1

You've incorrectly made the ArrayList a variable of type T. The line private T ArrayList; should be private ArrayList<T> l; where l would be the variable name.

The issue is that ArrayList is a generic type, so when you pass your generic type to it, you must pass it through ArrayList<T>. T ArrayList is a variable called ArrayList of type T.

You have to change this everywhere: every T you've shown in this snippet should be an ArrayList<T>.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.