initialize array elements during declaration

H

hyena

hi,

Just want to know how to initialize all the elements using construction
function in an array when I declare it.

I defined a class like

class A {

A(parameters....){
//do something here

}
}


in another class, I declare

....
A[] test = new A[2];

I want to pass some parameter at the same time, but i have no idea of where
shall i initialize the elements in the array. Any tips?


thanks in advance!

Sun
 
C

Collin VanDyck

hyena said:
hi,

Just want to know how to initialize all the elements using construction
function in an array when I declare it.

I defined a class like

class A {

A(parameters....){
//do something here

}
}


in another class, I declare

....
A[] test = new A[2];

I want to pass some parameter at the same time, but i have no idea of where
shall i initialize the elements in the array. Any tips?


thanks in advance!

Sun

When the JRE creates your array of A objects, the default behavior is to
set each element in the array to null. There is no way I know of to
auto-initialize each element of the array to be a non-null object.

You could address it this way:

class B
{
private A[] test = new A[2];

/**
* constructor
*/
public B(String param1, String param2)
{
for (int idx=0; idx<test.length; idx++)
{
test[idx] = new A(param1,param2);
}
}


}

Assuming that the params you pass into your container class (B) are the
same ones that A uses in its constructor.

Collin
 
J

John C. Bollinger

hyena said:
Just want to know how to initialize all the elements using construction
function in an array when I declare it.

I defined a class like

class A {

A(parameters....){
//do something here

}
}


in another class, I declare

....
A[] test = new A[2];

I want to pass some parameter at the same time, but i have no idea of where
shall i initialize the elements in the array. Any tips?

Note that your array declaration (above) declares the array, but doesn't
create any instances of A [all elements of array test are null].
Perhaps you want something like this:

A[] test = new A[] {new A("foo", "bar"),
new A("forty-two", "what's six times seven?")};

John Bollinger
(e-mail address removed)
 
B

Bob

DR said:
A[] test = new A[]
{
new A(...),
new A(...),
//...
}

Don't forget the semicolon to end the statement.

(Taking the 310-035 exam on Wednesday, so I'm on a syntax safari hunt.)
 
A

Andrew McDonagh

Bob said:
DR said:
A[] test = new A[]
{
new A(...),
new A(...),
//...
}


Don't forget the semicolon to end the statement.

(Taking the 310-035 exam on Wednesday, so I'm on a syntax safari hunt.)

Congrats on taking the exam, and please dont take the following as any
kind of insult to yourself....

But I can't stand exams or interview tests which test the persons
ability to read and write code, identifying or not producing syntax errors.

Twice in recent years I've not answered interview questions of this
sort, instead I've made it quiet clear to the employer that testing a
person for this ability is a waste of their time and mine.

Its a job for compilers.

I've created the Java tests for my current employer and there isn't a
single syntax problem to solve. Instead there's questions on general OO
approaches, Patterns and several on the mechanics of Java (i.e.
exception handling, Interfaces vs Abstract Classes, Inner classes etc).

*Phew* rant over...I'll get my coat...
 
D

DR

there are also the "..." that the compiler is likely not to appreciate :)



"Imagination is more important than knowledge" -- Albert Einstein.



Bob a écrit:
DR said:
A[] test = new A[]
{
new A(...),
new A(...),
//...
}


Don't forget the semicolon to end the statement.

(Taking the 310-035 exam on Wednesday, so I'm on a syntax safari hunt.)
 
R

Ryan Stewart

[...]
But I can't stand exams or interview tests which test the persons ability to
read and write code, identifying or not producing syntax errors.

Twice in recent years I've not answered interview questions of this sort,
instead I've made it quiet clear to the employer that testing a person for
this ability is a waste of their time and mine.

Its a job for compilers.

I've created the Java tests for my current employer and there isn't a single
syntax problem to solve. Instead there's questions on general OO approaches,
Patterns and several on the mechanics of Java (i.e. exception handling,
Interfaces vs Abstract Classes, Inner classes etc).

*Phew* rant over...I'll get my coat...
On one hand, I can agree with you. On the other, if a job requires the ability
to program in Java, I think you have to at least make sure someone knows basic
Java syntax, like maybe make sure he or she can write a Hello World program
without any help. Sun's reference compiler typically has excellent error
messages, yes. Not all compilers in all languages have that, and regardless, it
can be very, *very* beneficial to be able to look over a piece of code and say,
"Here's what's wrong with it..." without having to use anything else.
 
A

Andrew McDonagh

Ryan said:
[...]
But I can't stand exams or interview tests which test the persons ability to
read and write code, identifying or not producing syntax errors.

Twice in recent years I've not answered interview questions of this sort,
instead I've made it quiet clear to the employer that testing a person for
this ability is a waste of their time and mine.

Its a job for compilers.

I've created the Java tests for my current employer and there isn't a single
syntax problem to solve. Instead there's questions on general OO approaches,
Patterns and several on the mechanics of Java (i.e. exception handling,
Interfaces vs Abstract Classes, Inner classes etc).

*Phew* rant over...I'll get my coat...

On one hand, I can agree with you. On the other, if a job requires the ability
to program in Java, I think you have to at least make sure someone knows basic
Java syntax, like maybe make sure he or she can write a Hello World program
without any help. Sun's reference compiler typically has excellent error
messages, yes. Not all compilers in all languages have that, and regardless, it
can be very, *very* beneficial to be able to look over a piece of code and say,
"Here's what's wrong with it..." without having to use anything else.

Yes in other languages it can be beneficial, but in Java, the compilers
save us from this. therefore I'd rather the candidate knows about
technology e.g. SWT, Swing, CORBA, TDDing Mock Objects etc.

They still have to write simple mini apps for the tests, but we make it
clear that syntax errors will be ignored.

For instance, one question is to write a thread-safe singleton. This
test will help identify whether the candidate knows about Threading, The
Singleton Pattern, how to implement a singleton in Java with
thread-safety.

How they choose to implement it with regards to the varying
synchronisation mechanism available and how they instantiate the
singleton object is far more interesting to us, than whether they missed
a semi-colon, a '{' or a return type, etc.
 
B

Bob

Andrew said:
Congrats on taking the exam, and please dont take the following as any
kind of insult to yourself....

But I can't stand exams or interview tests which test the persons
ability to read and write code, identifying or not producing syntax errors.

I quite agree that not hiring someone on the strength of a missing
semicolon would be madness. But I have to say that, reading up on what I
need to know to pass the Programmer (syntax and core features) exam, it
has taught me how much I did not learn about Java in university. A lot
of things have been made clear, not just syntax snags. So I'm glad I've
studied it all. I just hope I don't get fazed on the day, and score too
low to pass.
I've created the Java tests for my current employer and there isn't a
single syntax problem to solve. Instead there's questions on general OO
approaches, Patterns and several on the mechanics of Java (i.e.
exception handling, Interfaces vs Abstract Classes, Inner classes etc).

Well, the syntax and behaviour of exception handling, assertions,
interfaces, abstract classes, inner classes (regular, anonymous,
method-local, and static nested) are all tested, and these are things I
would have cluelessly assumed I knew how to do. Now I think I can just
about do them all inside out. So I feel I've learned a lot.

Plus, if I pass the exam, I hope to then learn some OO theory and
practice, and take the Sun Java Developer exam. Do you have a book you
recommend for learning what can't be taught from a syntax diagram?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top