0

enter image description here

the code is below:

package linear_list; public class SeqList<T> { protected int n; protected T[] element; private static final int MIN_CAPACITY=16; public SeqList(int length){ if (length<MIN_CAPACITY) length=MIN_CAPACITY; this.element=new T[length]; //this row is reported an error this.n=0; } } 

Why IDEA shows that “Type parameter 'T' cannot be instantiated directly”. What I want to do is declare a generic array.

6
  • Generic are for compile time type safety, so if you initialize generic variable it should know the type of object at compile time. So generic type object can not be initialized this way. Rather you would need to take this as input from user. Commented Sep 6, 2021 at 12:35
  • 1
    Does this answer your question? What's the reason I can't create generic array types in Java? Commented Sep 6, 2021 at 12:35
  • You can do something like this T newT = (T[]) element.getClass().newInstance() Commented Sep 6, 2021 at 12:40
  • 1
    @ManeeshaIndrachapa you can't do that until element is initialized. Commented Sep 6, 2021 at 12:41
  • @maloomeister Yes Commented Sep 6, 2021 at 12:53

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.