Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Assigning Multiple Objects to One Array

 
Rick Rodriguez
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is the statement in my subject line possible? Perhaps I am missing it in my Java book, but can you assign more than one object to an array?
I understand that arrays are objects, etc., however take the following "object array" declaration/initialization:
buildings[] block = new buildings[100];
This would take a "buildings" object and place it to an array, referenced by the variable "block".
However, let us say that I wish to place "2" objects within one arrary. How can I declare/manipulate/etc. 2 objects within one array, instead of just 1, like in the example above.
This would apply to being able to declare/initialize 2 subclasses of a superclass in to one array. If "Sky" was an object that contained 2 subclasses, "buildings" and "grass", how can I declare the 'buildings' and 'grass' objects in to one array?
Any feedback would be greatly appreciated. Thank you.
 
Paul Stevens
Ranch Hand
Posts: 2823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could always use ArrayList or Vector. When you get them back out you will need to cast them to their appropriate type. They will be returned as Object.
 
Marilyn de Queiroz
Sheriff
Posts: 9059
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
>Is the statement in my subject line possible? can you assign
>more than one object to an array?

>I understand that arrays are objects ...

>However, let us say that I wish to place "2" objects within one
>array. How can I declare/manipulate/etc. 2 objects within one
>array, instead of just 1, like in the example above.
>This would apply to being able to declare/initialize 2
>subclasses of a superclass into one array. If "Sky" was an
>object that contained 2 subclasses, "buildings" and "grass", how
>can I declare the 'buildings' and 'grass' objects in to one
>array?
Where a Sky object, a Building object and a Grass object are subclasses of Thing:
Thing[] up = new Thing{ new Sky() , new Building() , new Grass() , new Thing() };
(Buildings and Grass are not Skys)

You need to have a sub/superclass relationship to be able to do this. Of course, any object can be stored in an Object[] array. The same is not true with primitives.
 
Rick Rodriguez
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Marilyn deQueiroz:
Where a Sky object, a Building object and a Grass object are subclasses of Thing:
Thing[] up = new Thing{ new Sky() , new Building() , new Grass() , new Thing() };
(Buildings and Grass are not Skys)

You need to have a sub/superclass relationship to be able to do this. Of course, any object can be stored in an Object[] array. The same is not true with primitives.
[/B]

In your example above, would that work if every object in the initialization brackets were subclasses of the "Thing" object?

I tried your example, and when I tried to make the statement:
up[0].getHeight();
Where "getHeight" is a public method in the "building" subclass, I get the compile error:
"ProgramName.java": Error #: 300 : method getHeight() not found in class jpb.Thing at line ##, column ##"
BTW - Your declaration above did not work. When initializing the array, you don't need the "new" keyword, so I did it this way:
Thing[] up = { new Sky() , new Building() , new Grass() , new Thing() };
Please let me know if anyone else has any other suggestions or if you can clarify my possible confusion at the latest post.
Thank you.
(edited by Cindy to format chart)
[This message has been edited by Cindy Glass (edited July 28, 2001).]
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure what you are trying to accomplish here.
You COULD make an array
Object[] anything = new Object[100];
Of course this will take every object in the world. That does NOT mean that you can manipulate the objects in anything with common methods, just the ones that they share because they all classes inherit from object.
In your example you can only invoke the objects of "up" using methods that all of the objects inherit from Thing. The method getHeight() would need to be in Thing, or you need to cast the Building object back to a Building before you access those methods so the system beleives that the method really IS there.
(Building)up[0].getHeight();
Better be sure that what is being held at [0] really IS a Building.
 
Rick Rodriguez
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Cindy Glass:
I'm not sure what you are trying to accomplish here.
You COULD make an array
Object[] anything = new Object[100];
Of course this will take every object in the world. That does NOT mean that you can manipulate the objects in anything with common methods, just the ones that they share because they all classes inherit from object.
In your example you can only invoke the objects of "up" using methods that all of the objects inherit from Thing. The method getHeight() would need to be in Thing, or you need to cast the Building object back to a Building before you access those methods so the system beleives that the method really IS there.
(Building)up[0].getHeight();
Better be sure that what is being held at [0] really IS a Building.

I tried doing that, but I got the following error:

Instead of making 2 subclasses for one superclass, I will simply make each of the 2 subclasses subclasses of one another, respectively, and this should solve my problem of accessing the various methods from the various subclasses.
Thank you all for your help.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic