• 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

generic method

 
Greenhorn
Posts: 20
Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi I need a solution how to create a generic method....
If it is an Integer ArrayList - it adds a random int variable into it and when it is a Double - it adds a random double...
So how to check whether ArrayList<T> is an instanceof Integer or Double and then how to add next random int/double into it...
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Slawomir Wieczorek wrote:Hi I need a solution how to create a generic method....
If it is an Integer ArrayList - it adds a random int variable into it and when it is a Double - it adds a random double...



Why would you need to do that?

So how to check whether ArrayList<T> is an instanceof Integer or Double and then how to add next random int/double into it...



You can't. At run-time they are both just ArrayList objects.

Of course there's probably some way to implement your actual requirement, but not the one you asked about here.
 
Slawomir Wieczorek
Greenhorn
Posts: 20
Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:
Why would you need to do that?


There is no need for that, just a training purpose
Let me be more precise ..
Suppose I have a class that stores objects of type Integer or Double (depending which one you choose) in the ArrayList:



So when instantiate such a class:

I will get an object that has an ArrayList<Integer> with 10 random numbers.....
Does it make any sense or rather there's something wrong with me....
 
Paul Clapham
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
The usual way for a generic class to know about one of its parameterized types is for the class to ask to be given that type at run-time. Like this:

Then to create one of those things:
 
Slawomir Wieczorek
Greenhorn
Posts: 20
Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Paul
 
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Those if (clazz == Foo.class) tests look very unlike object-oriented programming to me. It suggests you need to goback and reconsider your original design.
 
Happiness is not a goal ... it's a by-product of a life well lived - Eleanor Roosevelt. Tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic