aspose file tools*
The moose likes Java in General and the fly likes Generics Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Generics" Watch "Generics" New topic
Author

Generics

Greg Reeder
Ranch Hand

Joined: Jun 14, 2011
Posts: 99

Hi everyone!

I am looking for a good source to look up information regarding generics. More specific is this: On a JDialog, I want to have a JCombobox that houses two differant types of objects. The objects are similar, although they do not inherit from each other and, rather, both inherit from Object. Ok, more specific. I want to (in one combo) have a list of both of these objects (easy) and later (?) upcast (?) back up to the original object again. (hard) I need to be able to find out how to ask the object what it is so that I can perform the upcast(?). Does anyone know of a good source to look that up, or (if the code is a one liner, would mind sharing it with me?)? Thank you!!!

Greg
Shamayla Bukhari
Greenhorn

Joined: Jun 24, 2011
Posts: 4
Maybe what you are looking for is instanceof.


Greg Reeder
Ranch Hand

Joined: Jun 14, 2011
Posts: 99

is using instanceof performing the question "what are you"? So, I could say say, if (you instanceof person) then (eat) else if (you instanceof vegtable) then ( cook) ?
Shamayla Bukhari
Greenhorn

Joined: Jun 24, 2011
Posts: 4
Yes.
Greg Reeder
Ranch Hand

Joined: Jun 14, 2011
Posts: 99

Thank you!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40027
    
  28
Yes, but I don't think we have any good examples in this thread yet. You really ought to organise the types before you get to instanceof. There a re a few places where it is really useful. For example, if you go through the List interface, you find about twenty methods, which all appear in the ArrayList class, because ArrayList is a List. But there is another method which is only in ArrayList, so you can call it on an ArrayList and not on a LinkedList. So you would need to cast the List to an ArrayList, and instanceof allows you to check that cast. Look:Now, all this adding at the end will take time. ArrayLists are fast, but they like to have space. LinkedLists have no trouble adding at the end like that, but there is a method in ArrayList which makes adding millions of elements very very fast. But you can't call it on a Linked List. So you have to cast.:The ensureCapacity method isn't in List, nor in LinkedList, and the instanceof test ensures yoiu can cast without an Exception. So you can very quickly add those 1000000000 elements and just as quickly run out of memory
Another thing about ArrayLists: If you ever get those 1000000000 Foos into the list, you might want to look for a particular element. So you might want the get() method. But does get() work well? Look for the RandomAccess tagging interface. Now we can test for itYou will have read all about RandomAccess and know it means you can use get or a for loop without an Iterator to iterate the Collection. If fooList is a LinkedList, you would use the second method (in the else block) to find element 123456789.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14423
    
  23

Angelika Langer's Java Generics FAQ contains more than you ever wanted to know about generics.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Greg Reeder
Ranch Hand

Joined: Jun 14, 2011
Posts: 99

wow!! That is a lot of good information. (not needed in my current situation) but awesome!! Thanks. I will most definately need it later on.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Generics