This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of DevSecOps Adventures: A Game-Changing Approach with Chocolate, LEGO, and Coaching Games and have Dana Pylayeva on-line!
See this thread for details.
  • 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Tim Cooke
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
  • Piet Souris
Bartenders:
  • Stephan van Hulst

How to define the type of an Collection at run time

 
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
how to define the type of a collection at runtime.

For example,

if i have an Arraylist which has to be created based on the type, may be integer sometimes, or String or any object. I will know the type only at runtime.

I cannot declare the arraylist like ArrayList<String> arrList = new ArrayList<String>(), as this will not work for any other object type. I do not want to declare the arraylist raw as well.

Please suggest......
 
Marshal
Posts: 28304
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
You can't specify the generic type at run time.
 
Sheriff
Posts: 22809
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Correct. Search for "type erasure" to find out why.
 
Bartender
Posts: 2911
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
not exactly "run time", but this can help you:



My knowledge about generics is bad, someone please correct me if wrong...
 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
means anything and everything is excepted by the list.....

You wouldn't like to do this your type safety is gone for a toss unless you have very good reason to do so
 
salvin francis
Bartender
Posts: 2911
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Prabhat Jha wrote: means anything and everything is excepted by the list.....

You wouldn't like to do this your type safety is gone for a toss unless you have very good reason to do so



ye, i agree with that. I do not know a situation where i do not know what my collection will hold.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You cannot specify the type of a collection at runtime, and it is not useful to do so.

Java is a statically typed language. That means that the compiler needs to know all the types of the variables when you compile the program. If you don't know the type at compile time, you can't make use of Java's static type checking. If you know the type only at runtime, then that's too late for type checking by the compiler - because when your program is running, it has already been compiled.

So, there is no way to do this. It's like you want to eat the cake before you bake it.
 
Rob Spoor
Sheriff
Posts: 22809
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Prabhat Jha wrote: means anything and everything is excepted by the list.....


Actually, no. It means it can return anything and everything but you cannot add anything at all to the list. After all, the ? could in reality be String, Integer, Foo, ... The compiler can never know that what you add is allowed so it simply disallows everything.
 
Be reasonable. You can't destroy everything. Where would you sit? How would you read a tiny ad?
New web page for Paul's Rocket Mass Heaters movies
https://coderanch.com/t/785239/web-page-Paul-Rocket-Mass
reply
    Bookmark Topic Watch Topic
  • New Topic