Articles

Array of objects in Java

February 24, 2005 at 04:46:34
Specs: Win XP Pro SP1, 256mb DDR

I'm a beginner to Java. I need to create an array of objects. I have a class called child and a method to assign the age of a child. Then in my main method i want to create an array of child objects and assign a random age to each object in the array. How do you create an array that contains child objects?
I've tried things like:

children [i] = new child();

but i can't get it to work! Any help would be appreciated.

Regards, James


See More: Array of objects in Java

Report •


#1
February 24, 2005 at 05:07:06

isn't is something like

children[] = new child[i];

Chi

They mostly come at night...mostly


Report •

#2
February 24, 2005 at 07:56:55

I think it's the same as it is in C#:
child[] children = new child[i];

or the 2 line version:
child[] children;
children = new child[i];

-SN


Report •

#3
February 25, 2005 at 05:05:35

yeah, that's what i meant.

child[] children = new child[i];

They mostly come at night...mostly


Report •

Related Solutions

#4
March 9, 2005 at 20:56:21

hi james

solution is

child [] children=new child[5];
classname[] arrayname=new classname[no of objects];
then u write

child[0]=new child(23);
classname[objectnumber]=new classname(age);

child(23) is a parameterized construtor of class child.

this should solve ur problem...
do let me know when ur done


Aman


Report •


Ask Question