By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,738 Members | 2,016 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,738 IT Pros & Developers. It's quick & easy.

Initialising array and passing a constructor argument?

P: 2
Hi,
I'm sure this is a relatively simple question but I can't work out the syntax for what I want to do. I have a class whose constructor takes a single argument. If I declare an array of this class, I still want to pass the argument to the constructor but I can't seem to arrange the square brackets (number of items) and the curly brackets (constructor argument) and get it to compile. Can this be done in one line? How can it be done?

Example:

Expand|Select|Wrap|Line Numbers
  1. myClass singleInstance=new myClass(arg); // triggering the constructor fine
  2.  
  3. myClass[] x=new myClass[10]; //can't I put the (arg) here?
Have tried a few ways but am stumped and can't find an answer easily on the web.

Cheers, Tom
Jan 24 '07 #1
Share this Question
Share on Google+
3 Replies


10K+
P: 13,264
Hi,
I'm sure this is a relatively simple question but I can't work out the syntax for what I want to do. I have a class whose constructor takes a single argument. If I declare an array of this class, I still want to pass the argument to the constructor but I can't seem to arrange the square brackets (number of items) and the curly brackets (constructor argument) and get it to compile. Can this be done in one line? How can it be done?

Example:

Expand|Select|Wrap|Line Numbers
  1. myClass singleInstance=new myClass(arg); // triggering the constructor fine
  2.  
  3. myClass[] x=new myClass[10]; //can't I put the (arg) here?
Have tried a few ways but am stumped and can't find an answer easily on the web.

Cheers, Tom
You are creating an array of myClasses do you want to initialize them all with the same value? If so then
Expand|Select|Wrap|Line Numbers
  1. myClass[] x=new myClass[10]; 
  2. for(int i = 0; i < x.length;i++) {
  3.    myClass[i] = new myClass(arg);
  4. }
is the proper way of initialising the array
Jan 24 '07 #2

P: 2
Thanks for the reply.

I get this error with your code.
myEg.java:20: cannot find symbol
symbol : variable myClass
location: class myEg
myClass[i] = new myClass(arg)
;
myClass is the name of the class. I assume you meant:

Expand|Select|Wrap|Line Numbers
  1. for(int i = 0; i < x.length;i++) {
  2.    x[i] = new myClass(arg);
  3. }
which compiles fine.

And I assume that even if I wanted to pass different arguments I could do so by continuing:

Expand|Select|Wrap|Line Numbers
  1. x[1] = new myClass(arg);
  2. x[2] = new myClass(5);
  3. x[3] = new myClass(42);
and so on. Stupidly simple, I'm sure I'd tried that one at least once but obviously not. It is annoying that it can't be done in one line.

Cheers,

Tom
Jan 24 '07 #3

10K+
P: 13,264
Thanks for the reply.

I get this error with your code.
myEg.java:20: cannot find symbol
symbol : variable myClass
location: class myEg
myClass[i] = new myClass(arg)
;
myClass is the name of the class. I assume you meant:

Expand|Select|Wrap|Line Numbers
  1. for(int i = 0; i < x.length;i++) {
  2.    x[i] = new myClass(arg);
  3. }
which compiles fine.

And I assume that even if I wanted to pass different arguments I could do so by continuing:

Expand|Select|Wrap|Line Numbers
  1. x[1] = new myClass(arg);
  2. x[2] = new myClass(5);
  3. x[3] = new myClass(42);
and so on. Stupidly simple, I'm sure I'd tried that one at least once but obviously not. It is annoying that it can't be done in one line.

Cheers,

Tom
Indeed that's what I should have wrote. Good thing you pointed it out.
Good luck with the rest of it.
Jan 24 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.