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

How to create reference variables in a loop with incremental names - myRefVar1, myRefVar2, etc

 
AJ Taylor
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,

I've just started learning java and I'm trying to work out how i can create x n number of the same object (myObject) and assign them to a reference variable which the name increments by one each time, so i create myObject1, myObject2, myObject3 etc.

Here is the code. The issue is line #4 - i cannot use the String name i created on line 3, i'm stuck so help / suggestions are greatly appreciated.

Thanks,

AJ

 
Henry Wong
author
Marshal
Pie
Posts: 21003
77
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Will string arrays be useful here?

ie.

String myRefVar[] = new String[10];

Henry
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or more generally, arrays of any kind.

myObject[] objects = new myObject[10];

...

objects[i] = new myObject();
 
AJ Taylor
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:Will string arrays be useful here?

ie.

String myRefVar[] = new String[10];

Henry



Yes it would! Thanks guys!
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic