Using generics in Java

I just started using Java, so this may be a stupid question for most people,

Set<T> mySet;

Eclipse gives an error: the local variable myset may not have been initialized Than I tried to initialize it:

Set<T> mySet = new Set<T>();

But eclipse gives an error: "cannot instantiate type set."

What did I do wrong here?

Solution

Setting < T > is an interface and cannot be instantiated You can use HashSet < T >:

Set<T> set = new HashSet<T>();
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>