#### Archived

This topic is now archived and is closed to further replies.

# How Do I Create Multiple Objects Without Declaring Them, With A Loop?

This topic is 5317 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello All!, I am creating a loop that creates 6 clocks with random time zones. What I am unclear on is this: How do I create multiple objects without actually declaring them? I am trying to have a loop do it all for me. Do I do it by this?: Clock clock[y] ???? How do I add a letter or number to the end of the name of the object so it differs from the other? Thanks!
    for ( int y = 0; y < 7; y++){
Clock clock[y] = new Clock(timeZoneIds[random], xplace, yplace);
xplace += 100;
yplace += 100;
clock.draw(page);
}

[edited by - Klear on March 24, 2004 5:02:34 PM]

##### Share on other sites
Clock* clocks[7];for ( int y = 0; y < 7; y++){        clocks[y] = new Clock(timeZoneIds[random], xplace, yplace);xplace += 100;yplace += 100;clock.draw(page);    }

##### Share on other sites
Thanks for the reply, but I am getting errors...

ClockDriver.java [52:1] not a statement      Clock* clocks[7];           ^1 errorErrors compiling ClockDriver.

[edited by - Klear on March 24, 2004 5:17:37 PM]

##### Share on other sites
That might be because the replier posted C++ code?!

DISCLAIMER: If any of the above statements are incorrect, feel free to deliver me a good hard slap!

My games: DracMan | Swift blocks

##### Share on other sites
Clock ** clocks

clocks = new (Clock *)[7];

for(i=0;i<7;++i)
clocks = new Clock();

for(i=0;i<7;++i)
delete clocks[i];

delete[] clocks;

##### Share on other sites
Hmm yea your prolly right. = / This is Java source btw

##### Share on other sites
That s because you are using java and peter_b thought you used c++.

Clock clock[7];

for ( int y = 0; y < 7; y++){
clock[y] = new Clock(timeZoneIds[random], xplace, yplace);
xplace += 100;
yplace += 100;
clock[y].draw(page);
}

LizardCPP

[edited by - LizardCPP on March 24, 2004 5:24:03 PM]

##### Share on other sites
quote:
Original post by LizardCPP
That s because you are using java and peter_b thought you used c++.

 Clock clock[7];for ( int y = 0; y < 7; y++){ clock[y] = new Clock(timeZoneIds[random], xplace, yplace); xplace += 100; yplace += 100; clock[y].draw(page); }

LizardCPP

Thanks man but...

ClockDriver.java [52:1] '']'' expected      Clock clock[7];                  ^1 errorErrors compiling ClockDriver.

##### Share on other sites
quote:
Original post by Klear
quote:
Original post by LizardCPP
That s because you are using java and peter_b thought you used c++.

 Clock clock[7];for ( int y = 0; y < 7; y++){ clock[y] = new Clock(timeZoneIds[random], xplace, yplace); xplace += 100; yplace += 100; clock[y].draw(page); }

LizardCPP

Thanks man but...

ClockDriver.java [52:1] ']' expected      Clock clock[7];                  ^1 errorErrors compiling ClockDriver.

ah thats right....

you have to do Clock clock[] = new Clock[7]; first then the for-statement.

[edited by - LizardCPP on March 24, 2004 5:28:13 PM]

##### Share on other sites
quote:
Original post by LizardCPP
quote:
Original post by Klear
quote:
Original post by LizardCPP
That s because you are using java and peter_b thought you used c++.

 Clock clock[7];for ( int y = 0; y < 7; y++){ clock[y] = new Clock(timeZoneIds[random], xplace, yplace); xplace += 100; yplace += 100; clock[y].draw(page); }

LizardCPP

Thanks man but...

ClockDriver.java [52:1] ']' expected      Clock clock[7];                  ^1 errorErrors compiling ClockDriver.

ah thats right....

you have to do Clock clock = new Clock[7]; first then the for-statement.

Thanks for hanging in Lizard Now its saying that I need an array? Are the brackets not doing that?

   ClockDriver.java [52:1] incompatible typesfound   : Clock[]required: Clock      Clock clock = new Clock[7];                    ^ClockDriver.java [55:1] array required, but Clock found        clock[y] = new Clock(timeZoneIds[random], xplace, yplace);             ^ClockDriver.java [58:1] array required, but Clock found        clock[y].draw(page);             ^3 errorsErrors compiling ClockDriver.

[edited by - Klear on March 24, 2004 5:28:41 PM]

[edited by - Klear on March 24, 2004 5:29:22 PM]

1. 1
2. 2
3. 3
4. 4
Rutin
17
5. 5

• 14
• 9
• 10
• 12
• 17
• ### Forum Statistics

• Total Topics
632906
• Total Posts
3009157
• ### Who's Online (See full list)

There are no registered users currently online

×