Array declaration syntax question

Discussion in 'Java' started by barry, Oct 15, 2003.

  1. barry

    barry Guest

    Hi - another silly Java syntax question. I have an 2D array, one
    dimension I won't know the size of until runtime, the other dimension
    is always 8. I have tried the following declaration

    private short[8][] myArray=null;

    but the compiler laughs at me. After much trying, I have to ask the NG
    - what is the magic syntax?

    Thank you
    Barry
     
    barry, Oct 15, 2003
    #1
    1. Advertising

  2. barry wrote:
    > Hi - another silly Java syntax question. I have an 2D array, one
    > dimension I won't know the size of until runtime, the other dimension
    > is always 8. I have tried the following declaration
    >
    > private short[8][] myArray=null;
    >
    > but the compiler laughs at me. After much trying, I have to ask the NG
    > - what is the magic syntax?


    The size of a dimension is a property of an actual object, not of its tpye,
    so you don't have to declare it as part of the type. Furthermore,
    multidimensional arrays in Java are in fact arrays of arrays, so it looks
    like this:

    private short[][] myArray = new short[8][];
    myArray[0] = new short[x];
     
    Michael Borgwardt, Oct 15, 2003
    #2
    1. Advertising

  3. On 15 Oct 2003 02:40:35 -0700, barry wrote:
    > Hi - another silly Java syntax question. I have an 2D array, one
    > dimension I won't know the size of until runtime, the other dimension
    > is always 8. I have tried the following declaration
    >
    > private short[8][] myArray=null;
    >
    > but the compiler laughs at me. After much trying, I have to ask the NG
    > - what is the magic syntax?


    This isn't C. You specify the dimensions when you initialize the
    array, not as part of the type declaration.

    private short[][] myArray;

    int n = foo();
    myArray = new short[8][n];

    /gordon

    --
    [ do not email me copies of your followups ]
    g o r d o n + n e w s @ b a l d e r 1 3 . s e
     
    Gordon Beaton, Oct 15, 2003
    #3
  4. barry

    Sudsy Guest

    barry wrote:
    > Hi - another silly Java syntax question. I have an 2D array, one
    > dimension I won't know the size of until runtime, the other dimension
    > is always 8. I have tried the following declaration
    >
    > private short[8][] myArray=null;
    >
    > but the compiler laughs at me. After much trying, I have to ask the NG
    > - what is the magic syntax?
    >
    > Thank you
    > Barry


    Here's an example:

    public class x {

    private short[][] myArray = new short[8][];

    public x() {
    for( int i = 0; i < myArray.length; i++ )
    myArray = new short[i + 1];
    }

    ...

    }

    In this case I just instantitate an array of i + 1 elements
    and assign them to myArray such that I have the following
    array elements defined:

    myArray[0][0]
    myArray[1][0]
    myArray[1][1]
    myArray[2][0]
    myArray[2][1]
    myArray[2][2]
    ....up to...
    myArray[7][7]
     
    Sudsy, Oct 15, 2003
    #4
  5. barry wrote:

    > Hi - another silly Java syntax question. I have an 2D array, one
    > dimension I won't know the size of until runtime, the other dimension
    > is always 8. I have tried the following declaration
    >
    > private short[8][] myArray=null;
    >
    > but the compiler laughs at me. After much trying, I have to ask the NG
    > - what is the magic syntax?


    It's a multi-dimensional array, so the type is:

    short[][]

    The type will have no reference to the number of elements. That is defined
    when the actual array is allocated.

    --
    Darryl L. Pierce <>
    Visit the Infobahn Offramp - <http://bellsouthpwp.net/m/c/mcpierce>
    "What do you care what other people think, Mr. Feynman?"
     
    Darryl L. Pierce, Oct 15, 2003
    #5
  6. Michael Borgwardt <> coughed up the following:

    > barry wrote:
    >> Hi - another silly Java syntax question. I have an 2D array, one
    >> dimension I won't know the size of until runtime, the other dimension
    >> is always 8. I have tried the following declaration
    >>
    >> private short[8][] myArray=null;
    >>
    >> but the compiler laughs at me. After much trying, I have to ask the
    >> NG - what is the magic syntax?

    >
    > The size of a dimension is a property of an actual object, not of its
    > tpye, so you don't have to declare it as part of the type.
    > Furthermore, multidimensional arrays in Java are in fact arrays of
    > arrays, so it looks like this:
    >
    > private short[][] myArray = new short[8][];
    > myArray[0] = new short[x];


    Note that such and similar syntax is unnecessary. This works just fine:

    int xSize = 7;
    int a[][] = new int[5][xSize];

    as verified by the following

    public static void main(String[] args)
    {
    int xSize = 7;
    int a[][] = new int[5][xSize];

    for (int y=0; y<5; y++)
    for (int x=0; x<xSize; x++)
    a[y][x] = 9;

    for (int y=0; y<5; y++)
    {
    for (int x=0; x<xSize; x++)
    System.out.print(a[y][x]+" ");
    System.out.println();
    }
    System.out.println();
    }

    Which produces:

    9 9 9 9 9 9 9
    9 9 9 9 9 9 9
    9 9 9 9 9 9 9
    9 9 9 9 9 9 9
    9 9 9 9 9 9 9
     
    Thomas G. Marshall, Oct 15, 2003
    #6
  7. barry

    Roedy Green Guest

    Roedy Green, Oct 16, 2003
    #7
  8. Thomas G. Marshall wrote:
    >>private short[][] myArray = new short[8][];
    >>myArray[0] = new short[x];

    >
    >
    > Note that such and similar syntax is unnecessary. This works just fine:
    >
    > int xSize = 7;
    > int a[][] = new int[5][xSize];


    True, but does not allow you do have "jagged arrays" where the second dimension's
    size varies within the array. Admittedly, it seems like barry didn't need that.
     
    Michael Borgwardt, Oct 16, 2003
    #8
  9. barry

    Phil... Guest

    My book says you can make jagged arrays
    after they have been declaired.
    I mean you can have abc[0][5]
    abc[1][9]
    etc.

    "Michael Borgwardt" <> wrote in message
    news:bmlijr$obofa$-berlin.de...
    > Thomas G. Marshall wrote:
    > >>private short[][] myArray = new short[8][];
    > >>myArray[0] = new short[x];

    > >
    > >
    > > Note that such and similar syntax is unnecessary. This works just fine:
    > >
    > > int xSize = 7;
    > > int a[][] = new int[5][xSize];

    >
    > True, but does not allow you do have "jagged arrays" where the second

    dimension's
    > size varies within the array. Admittedly, it seems like barry didn't need

    that.
    >
     
    Phil..., Oct 17, 2003
    #9
  10. Phil... wrote:
    > My book says you can make jagged arrays
    > after they have been declaired.
    > I mean you can have abc[0][5]
    > abc[1][9]
    > etc.


    That's exactly my point: you can do that, but only with the
    method I described, not with the one Thomss described.
     
    Michael Borgwardt, Oct 17, 2003
    #10
  11. Michael Borgwardt <> coughed up the following:

    > Phil... wrote:
    >> My book says you can make jagged arrays
    >> after they have been declaired.
    >> I mean you can have abc[0][5]
    >> abc[1][9]
    >> etc.

    >
    > That's exactly my point: you can do that, but only with the
    > method I described, not with the one Thomss described.


    You're wrong, you /can/ with the method I described.

    It's useful, for example if what you want is a rectangle matrix in every
    regard except for a few rows, which are of different sizes.

    This is produced:

    9 9 9 9 9 9 9
    9 9 9 9 9 9 9
    9 9 9 9 9 9 9 0 0 0 0 0 0 0 0
    9 9 9 9 9 9 9
    9 9 9 9 9 9 9

    by the following code:

    int xSize = 7;
    int a[][] = new int[5][xSize];

    a[2] = new int[15]; // <------NOTE------

    for (int y=0; y<5; y++)
    for (int x=0; x<xSize; x++)
    a[y][x] = 9;

    for (int y=0; y < a.length; y++)
    {
    for (int x=0; x < a[y].length; x++)
    System.out.print(a[y][x]+" ");
    System.out.println();
    }
    System.out.println();

    Besides, my original point was that your solution was unnecessary given the
    OP's question. Others had already given an adequate explanation of the
    array of arrays topic, so I filled in what you had missed.
     
    Thomas G. Marshall, Oct 17, 2003
    #11
  12. Thomas G. Marshall <>
    coughed up the following:

    ....[self snippage]...

    [Remember that this technique is in addition to the other
    techniques explained, not in replacement of. In addition,
    this technique is particularly well suited to the need for
    box arrays with only a /few/ rows of odd lengths.]

    It occurred to me that my example might not be enough for a newbie. I
    specifically fill a box with 9 and then print the entirety of the array of
    arrays, which has a leg of 15 elements, which are 0, to show the parts of
    the original box array and the extra elements of row 2.

    Perhaps it would have been clearer if I had filled the entire thing with
    9's. This is an example where the fill loop is changed to:

    for (int y=0; y < a.length; y++)
    for (int x=0; x < a[y].length; x++)
    a[y][x] = 9;

    Producing:

    9 9 9 9 9 9 9
    9 9 9 9 9 9 9
    9 9 9 9 9 9 9 9 9 9 9 9 9 9 9
    9 9 9 9 9 9 9
    9 9 9 9 9 9 9

    ....and this is all the code:

    int xSize = 7;
    int a[][] = new int[5][xSize];

    a[2] = new int[15];

    for (int y=0; y < a.length; y++)
    for (int x=0; x < a[y].length; x++)
    a[y][x] = 9;

    for (int y=0; y < a.length; y++)
    {
    for (int x=0; x < a[y].length; x++)
    System.out.print(a[y][x]+" ");
    System.out.println();
    }
    System.out.println();
     
    Thomas G. Marshall, Oct 17, 2003
    #12
    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. Noah
    Replies:
    5
    Views:
    970
  2. Replies:
    1
    Views:
    401
    Peter Davis
    Dec 14, 2004
  3. Alex Vinokur
    Replies:
    4
    Views:
    604
    Jonathan Turkanis
    Apr 5, 2004
  4. Ovidesvideo
    Replies:
    4
    Views:
    528
    Andrey Tarasevich
    Dec 10, 2004
  5. SpreadTooThin
    Replies:
    1
    Views:
    323
    Victor Bazarov
    Mar 29, 2007
Loading...

Share This Page