Results 1 to 4 of 4

Thread: [RESOLVED] This array doesn't work.

  1. #1

    Thread Starter
    Hyperactive Member gjon's Avatar
    Join Date
    Nov 2004
    Location
    Inescapable Void
    Posts
    442

    Resolved [RESOLVED] This array doesn't work.

    The book example of this array has an error when I try to run it.
    int[][] x = { { 2, 3 }, { 4, 5 }, { 6, 7 } };
    error:
    Array initializers can only be used in a variable or field initializer. Try using a new expression instead.

    Ok, after looking at the MSDN, I think it should look like:
    int[,] x = new int[3, 2];

    Is the book example above(up top) even correct?

  2. #2
    G&G Moderator chemicalNova's Avatar
    Join Date
    Jun 2002
    Location
    Victoria, Australia
    Posts
    4,246

    Re: This array doesn't work.

    Which one are you wanting? The Multidimensional array or the Jagged array? The first one with the error looks more suited to a mutlidimensional array, which is what you declared in the second one:
    PHP Code:
    int[,] = new int[32] { {23}, {45}, {67} }; 
    ?

    EDIT: Also, jagged arrays would be declared like so:
    PHP Code:
    int[][] = new int[][] { new int[] {2,3,4}, new int[] {5,6,7} }; 
    chem

    Visual Studio 6, Visual Studio.NET 2005, MASM

  3. #3

    Thread Starter
    Hyperactive Member gjon's Avatar
    Join Date
    Nov 2004
    Location
    Inescapable Void
    Posts
    442

    Re: This array doesn't work.

    I just wanted to be sure that the first one is defenitely an error before I go marking in the book to fix it.
    I will replace it with the second line that I believe to be the correct version of the multidimensional array.

    In your line:
    int[][] x = new int[][] { new int[] {2,3,4}, new int[] {5,6,7} };

    You are only initializing part of the jagged array? That is ok to do? What happens with the empty index when the array gets used?

  4. #4

    Thread Starter
    Hyperactive Member gjon's Avatar
    Join Date
    Nov 2004
    Location
    Inescapable Void
    Posts
    442

    Re: This array doesn't work.

    Ok, it defenitely was incorrect. It has to have the new.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width