aspose file tools*
The moose likes Beginning Java and the fly likes Assigning Multiple Objects to One Array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Assigning Multiple Objects to One Array" Watch "Assigning Multiple Objects to One Array" New topic
Author

Assigning Multiple Objects to One Array

Rick Rodriguez
Ranch Hand

Joined: Jun 02, 2001
Posts: 44
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

Joined: May 17, 2001
Posts: 2823
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

Joined: Jul 22, 2000
Posts: 9047
    
  10
>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.


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Rick Rodriguez
Ranch Hand

Joined: Jun 02, 2001
Posts: 44
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

Joined: Sep 29, 2000
Posts: 8521
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.

"JavaRanch, where the deer and the Certified play" - David O'Meara
Rick Rodriguez
Ranch Hand

Joined: Jun 02, 2001
Posts: 44
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Assigning Multiple Objects to One Array