Computing.Net > Forums > Programming > Array of objects in Java

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Array of objects in Java

Reply to Message Icon

Name: SergeantFuzz
Date: February 24, 2005 at 04:46:34 Pacific
OS: Win XP Pro SP1
CPU/Ram: 256mb DDR
Comment:

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



Sponsored Link
Ads by Google

Response Number 1
Name: Chi Happens
Date: February 24, 2005 at 05:07:06 Pacific
Reply:

isn't is something like

children[] = new child[i];

Chi

They mostly come at night...mostly


0

Response Number 2
Name: SN
Date: February 24, 2005 at 07:56:55 Pacific
Reply:

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


0

Response Number 3
Name: Chi Happens
Date: February 25, 2005 at 05:05:35 Pacific
Reply:

yeah, that's what i meant.

child[] children = new child[i];

They mostly come at night...mostly


0

Response Number 4
Name: Amandeep
Date: March 9, 2005 at 20:56:21 Pacific
Reply:

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


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Array of objects in Java

sorting an array of string in java www.computing.net/answers/programming/sorting-an-array-of-string-in-java/18011.html

Array of Objects 2 www.computing.net/answers/programming/array-of-objects-2/14653.html

How to create objects in java? www.computing.net/answers/programming/how-to-create-objects-in-java/18740.html