• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Generics: Implementing a set class

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 15510
363
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic