Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > initialize array elements during declaration

Reply
Thread Tools

initialize array elements during declaration

 
 
hyena
Guest
Posts: n/a
 
      01-24-2005
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


 
Reply With Quote
 
 
 
 
Collin VanDyck
Guest
Posts: n/a
 
      01-24-2005
hyena wrote:
> 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

 
Reply With Quote
 
 
 
 
DR
Guest
Posts: n/a
 
      01-24-2005
A[] test = new A[]
{
new A(...),
new A(...),
//...
}



hyena a écrit:
> 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
>
>


 
Reply With Quote
 
hyena
Guest
Posts: n/a
 
      01-24-2005
Thanks you guys very much!


 
Reply With Quote
 
John C. Bollinger
Guest
Posts: n/a
 
      01-24-2005
hyena wrote:

> 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
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
Bob
Guest
Posts: n/a
 
      01-24-2005
DR wrote:
> 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.)
--
Bob
 
Reply With Quote
 
Andrew McDonagh
Guest
Posts: n/a
 
      01-24-2005
Bob wrote:
> DR wrote:
>
>> 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...



 
Reply With Quote
 
DR
Guest
Posts: n/a
 
      01-24-2005
there are also the "..." that the compiler is likely not to appreciate



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



Bob a écrit:
> DR wrote:
>
>> 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.)


 
Reply With Quote
 
Ryan Stewart
Guest
Posts: n/a
 
      01-24-2005
"Andrew McDonagh" <(E-Mail Removed)2s.com> wrote in message
news:ct3c8d$gg9$(E-Mail Removed)2surf.net...
[...]
> 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.


 
Reply With Quote
 
Andrew McDonagh
Guest
Posts: n/a
 
      01-24-2005
Ryan Stewart wrote:
> "Andrew McDonagh" <(E-Mail Removed)2s.com> wrote in message
> news:ct3c8d$gg9$(E-Mail Removed)2surf.net...
> [...]
>
>>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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Initialize a static array with struct elements syang8 C++ 4 06-21-2008 09:32 AM
initialize an array of elements that contain another array wenmang@yahoo.com C Programming 10 08-02-2006 04:52 PM
if instance variable get initialize after assigning some values or after constructor then when does static variable get initialize Tony Morris Java 3 02-04-2006 08:39 AM
Initialize elements in an array Mars C Programming 5 01-28-2005 01:02 PM
constant array size during declaration ?? news.hku.hk C++ 7 04-25-2004 01:22 PM



Advertisments