• Post Reply Bookmark Topic Watch Topic
  • New Topic

Generics: Implementing a set class  RSS feed

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I'm trying to implement a generic class used to define a set of different types. I'm trying to add a constructor that stores an array into the set. for some reason the add method does not work. (The compiler says: "Add (E) in arrayList can not be applied to (E)"

Here is my code:

Any idea why It won't add the elements?

Thanks
 
Saloon Keeper
Posts: 7993
143
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because you are redeclaring the type parameter E in your method.

Why are you using such a method anyway? Why not provide a constructor that takes an array?
 
Gil Shoam
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:

Why are you using such a method anyway? Why not provide a constructor that takes an array?


You are right.. that is what I meant to do.....

(it's 2:30 a.m here... )

but anyway can you elaborate on this:

Stephan van Hulst wrote:Because you are redeclaring the type parameter E in your method.

 
Sheriff
Posts: 22846
43
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First you declare a type variable called <E> in line 1. Then you declare another type variable in line 11. It's also called <E>, which leads to all kinds of confusion, especially since it isn't the same as the one in line 1.

Presumably you wanted to use the <E> declared at the class level. In which case don't declare another <E> at the method level.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!