• Post Reply Bookmark Topic Watch Topic
  • New Topic

Resizable Collection with Different Types/Classes  RSS feed

 
Greenhorn
Posts: 12
Eclipse IDE Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am looking for a way to put references to objects from different classes into an array-like collection that is resizable.

I am aware that this is viewed by some as an unwise practice, but I'm all about stupid.

I've tried ArrayList, but it seems to store elements as Object types, regardless of what the original objects are that the elements refer to.

I've done some initial reading on generics, but the impression I have so far is that generics are used to add type safety to collections (so instead of storing references to Objects, your collection can store references to whatever type/class you want - but only one type. (Or, at least it doesn't seem to allow you to designate that myArrayList[0] should be an instance of Apple and myArrayList[1] should be an instance of Orange.)

I want to be able to do this kind of stuff:



That way, I can have a master list of all objects in a certain subset of my program and efficiently (at least coding-wise) access member functions without mixing up apples, oranges, and bricks.

In the recently condemned world of ActionScript 3.0, this is easily done, and I think it's possible in C#, so I'm hoping there exists a workaround for it in Java, regardless of whatever poor programming practices it might involve.
 
Ranch Hand
Posts: 445
1
Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

In case you do not want to store objects of similar nature in your Collection instance and want to permit the Collection to store a varied range of Objects, you could use the below:



But this way would result in an awful lot of if-else statements. This could result in hard to maintain code and hell-of-a-lot-of redundancy.

You might also handle it in an Object oriented fashion by making your domain/application objects have a parent class which has generic methods and the derived objects provide the overridden methods for specialization. So invoking any method would result in the corresponding method in the derived class being called.

Does this suggestion help?

Cheers,
Raj.
 
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony Tirolese wrote:In the recently condemned world of ActionScript 3.0, this is easily done, and I think it's possible in C#, so I'm hoping there exists a workaround for it in Java, regardless of whatever poor programming practices it might involve.

It's easily done in Java too. The problem (and the reason it's generally regarded as a bad practise) is not storing the objects, but how you get them back. What's your plan of action for that?

Winston

 
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Tony. Welcome to the Ranch!

You could always specialise ArrayList to do some of the grunt work for you:


Then you'd use it like this:

Note, I'm not saying it's a good idea. There's almost certainly a better solution to your problem that doesn't involve trying to do this.
 
Ranch Hand
Posts: 177
Hibernate Linux Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Question is, for what are you using it?
If you need to call methods on those objects, I would declare an interface and let those objects implement them.
Then you can do what you did to add them to the collection and call the method on every collection element.
Easiest, best way to do it imho.
 
Tony Tirolese
Greenhorn
Posts: 12
Eclipse IDE Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew Brown wrote:Hi Tony. Welcome to the Ranch!

You could always specialise ArrayList to do some of the grunt work for you:


Then you'd use it like this:

Note, I'm not saying it's a good idea. There's almost certainly a better solution to your problem that doesn't involve trying to do this.


Thanks, that'll work!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!