pass an array as a function argument

Discussion in 'Java' started by Matt, Jul 28, 2004.

  1. Matt

    Matt Guest

    when we pass an array as a function argument, we do this:

    printArray(new int[] {4,5,6}); //good

    OR

    int [] a = {4,5,6};
    printArray(a); //good

    However, if we declare an array of integers like this, it will yield compile error:
    int [] a = new int {1,2,3}; //compile error

    what's the rationales? please advise. thanks!!
     
    Matt, Jul 28, 2004
    #1
    1. Advertising

  2. Matt

    Murray Guest

    "Matt" <> wrote in message
    news:...
    > when we pass an array as a function argument, we do this:
    >
    > printArray(new int[] {4,5,6}); //good
    >
    > OR
    >
    > int [] a = {4,5,6};
    > printArray(a); //good
    >
    > However, if we declare an array of integers like this, it will yield

    compile error:
    > int [] a = new int {1,2,3}; //compile error


    I think you mean

    int[] a = new int[] {1,2,3}; // you forgot the second []
     
    Murray, Jul 28, 2004
    #2
    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. Ben Kial
    Replies:
    1
    Views:
    700
    Eric Enright
    Nov 15, 2004
  2. S?ren Gammelmark
    Replies:
    1
    Views:
    1,964
    Eric Sosman
    Jan 7, 2005
  3. Reckoner
    Replies:
    11
    Views:
    726
    Steven D'Aprano
    Jan 19, 2009
  4. AikidoGuy
    Replies:
    11
    Views:
    584
    Seebs
    Nov 21, 2011
  5. Replies:
    8
    Views:
    128
Loading...

Share This Page