initialize array elements during declaration

Discussion in 'Java' started by hyena, Jan 24, 2005.

  1. hyena

    hyena Guest

    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
    hyena, Jan 24, 2005
    #1
    1. Advertising

  2. 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
    Collin VanDyck, Jan 24, 2005
    #2
    1. Advertising

  3. hyena

    DR Guest

    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
    >
    >
    DR, Jan 24, 2005
    #3
  4. hyena

    hyena Guest

    Thanks you guys very much!
    hyena, Jan 24, 2005
    #4
  5. 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
    John C. Bollinger, Jan 24, 2005
    #5
  6. hyena

    Bob Guest

    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
    Bob, Jan 24, 2005
    #6
  7. 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...
    Andrew McDonagh, Jan 24, 2005
    #7
  8. hyena

    DR Guest

    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.)
    DR, Jan 24, 2005
    #8
  9. hyena

    Ryan Stewart Guest

    "Andrew McDonagh" <2s.com> wrote in message
    news:ct3c8d$gg9$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.
    Ryan Stewart, Jan 24, 2005
    #9
  10. Ryan Stewart wrote:
    > "Andrew McDonagh" <2s.com> wrote in message
    > news:ct3c8d$gg9$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.
    Andrew McDonagh, Jan 24, 2005
    #10
  11. hyena

    Bob Guest

    Andrew McDonagh wrote:

    > 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?
    --
    Bob
    Bob, Jan 24, 2005
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. news.hku.hk
    Replies:
    7
    Views:
    11,235
    Jeff Schwab
    Apr 25, 2004
  2. Mars

    Initialize elements in an array

    Mars, Jan 27, 2005, in forum: C Programming
    Replies:
    5
    Views:
    380
    infobahn
    Jan 28, 2005
  3. Replies:
    10
    Views:
    675
  4. syang8
    Replies:
    4
    Views:
    718
    James Kanze
    Jun 21, 2008
  5. Peskov Dmitry
    Replies:
    4
    Views:
    316
Loading...

Share This Page