Two dimensional array initialization

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Dadi

    Two dimensional array initialization

    Hi,

    I can make a simple initialization work like this:

    Object[,] ONE_ROW = {{"Vodafone", "55501601001970 16"}};

    But, now I want to create another array that consists of multiple copies of ONE_ROW like this (will not compile):

    Object[,] TWO_ROWS = {ONE_ROW, ONE_ROW}; // Works in Java ;-)

    This yields "Incorrectl y structured array initializer"!!

    This, however, is a correct implementation using hard-coded data:

    Object[,] TWO_ROWS = {{"Vodafone", "55501601001970 16"}, {"Vodafone", "55501601001970 16"}};

    Any ideas?

    Regards,
    Dadi.
  • Jon Skeet [C# MVP]

    #2
    Re: Two dimensional array initialization

    Dadi <dadi@hugur.i s> wrote:[color=blue]
    > I can make a simple initialization work like this:
    >
    > Object[,] ONE_ROW = {{"Vodafone", "55501601001970 16"}};
    >
    > But, now I want to create another array that consists of multiple copies
    > of ONE_ROW like this (will not compile):[/color]

    It sounds like you want jagged arrays then, rather than rectangular
    arrays. If you do:

    Object[][,] TWO_ROWS = {ONE_ROW, ONE_ROW}; I believe it'll work.

    (Haven't tried it though...)

    --
    Jon Skeet - <skeet@pobox.co m>
    Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

    If replying to the group, please do not mail me too

    Comment

    • Daniel Billingsley

      #3
      Re: Two dimensional array initialization

      Actually Jon I think he wants

      Object[][] TWO_ROWS =

      a jagged array of ONE_ROW arrays. Your code would be a jagged array of 2D
      rectangular arrays which I don't think was the intent, wouldn't it?

      I've been wrong about arrays before so maybe I am again.

      "Jon Skeet [C# MVP]" <skeet@pobox.co m> wrote in message
      news:MPG.19ed1c 1f41a818b498981 9@msnews.micros oft.com...[color=blue]
      > Dadi <dadi@hugur.i s> wrote:[color=green]
      > > I can make a simple initialization work like this:
      > >
      > > Object[,] ONE_ROW = {{"Vodafone", "55501601001970 16"}};
      > >
      > > But, now I want to create another array that consists of multiple copies
      > > of ONE_ROW like this (will not compile):[/color]
      >
      > It sounds like you want jagged arrays then, rather than rectangular
      > arrays. If you do:
      >
      > Object[][,] TWO_ROWS = {ONE_ROW, ONE_ROW}; I believe it'll work.
      >
      > (Haven't tried it though...)
      >
      > --
      > Jon Skeet - <skeet@pobox.co m>
      > http://www.pobox.com/~skeet
      > If replying to the group, please do not mail me too[/color]


      Comment

      • Jon Skeet [C# MVP]

        #4
        Re: Two dimensional array initialization

        Daniel Billingsley <dbillingsley@N O.durcon.SPAAMM .com> wrote:[color=blue]
        > Actually Jon I think he wants
        >
        > Object[][] TWO_ROWS =
        >
        > a jagged array of ONE_ROW arrays. Your code would be a jagged array of 2D
        > rectangular arrays which I don't think was the intent, wouldn't it?[/color]

        I don't know - the intent wasn't very clear! However, my code would
        give a single array of 2D rectangular arrays.

        --
        Jon Skeet - <skeet@pobox.co m>
        Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

        If replying to the group, please do not mail me too

        Comment

        • Daniel Billingsley

          #5
          Re: Two dimensional array initialization

          That's what I said, isn't it? :)

          I meant your overall array [][,] would be jagged, as opposed to simply 3D
          [,,].

          Yes, no, maybe so? (Trying to see if I'm starting to grasp this terminology
          as well as I think I am.)

          "Jon Skeet [C# MVP]" <skeet@pobox.co m> wrote in message
          news:MPG.19efa7 9e5318480a98984 3@msnews.micros oft.com...
          [color=blue][color=green]
          > > ... Your code would be a jagged array of 2D
          > > rectangular arrays which I don't think was the intent, wouldn't it?[/color]
          >
          > I don't know - the intent wasn't very clear! However, my code would
          > give a single array of 2D rectangular arrays.
          >[/color]


          Comment

          • Jon Skeet [C# MVP]

            #6
            Re: Two dimensional array initialization

            Daniel Billingsley <dbillingsley@N O.durcon.SPAAMM .com> wrote:[color=blue]
            > That's what I said, isn't it? :)[/color]

            Not quite - it's definitely *not* a jagged array *of* 2D rectangular
            arrays, in the same way that int[] isn't a jagged array of ints, it's
            just an array of ints, if you see what I mean.
            [color=blue]
            > I meant your overall array [][,] would be jagged, as opposed to simply 3D
            > [,,].[/color]

            I'm not sure whether I'd say the overall array is jagged or not - it's
            certainly not rectangular, but it's not jagged in the normal sense
            either.

            Fortunately this is unlikely to actually be an issue very often :)

            --
            Jon Skeet - <skeet@pobox.co m>
            Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

            If replying to the group, please do not mail me too

            Comment

            • Daniel Billingsley

              #7
              Re: Two dimensional array initialization

              Hmm... I thought jagged simply meant an array of arrays... what did you mean
              by the "normal sense" of jagged usage?

              an array of ints is 1 dimensional, and not relevant to this discussion in my
              mind.

              an array of arrays, or an array of arrays of arrays, or an array of 2D
              arrays, are all "jagged" I would think by definition, no?

              [,] defines 2D array, definitely not jagged
              [][] defines array of arrays, definitely jagged

              [][,] defines an array of 2D arrays, jagged, right?
              [][][] defines an array of arrays of arrays, jagged, right?
              [,,] defines a 3D array, not jagged, right?

              "Jon Skeet [C# MVP]" <skeet@pobox.co m> wrote in message
              news:MPG.19efdf 018284f12f98984 4@msnews.micros oft.com...[color=blue]
              > Daniel Billingsley <dbillingsley@N O.durcon.SPAAMM .com> wrote:[color=green]
              > > That's what I said, isn't it? :)[/color]
              >
              > Not quite - it's definitely *not* a jagged array *of* 2D rectangular
              > arrays, in the same way that int[] isn't a jagged array of ints, it's
              > just an array of ints, if you see what I mean.
              >[color=green]
              > > I meant your overall array [][,] would be jagged, as opposed to simply[/color][/color]
              3D[color=blue][color=green]
              > > [,,].[/color]
              >
              > I'm not sure whether I'd say the overall array is jagged or not - it's
              > certainly not rectangular, but it's not jagged in the normal sense
              > either.
              >
              > Fortunately this is unlikely to actually be an issue very often :)
              >
              > --
              > Jon Skeet - <skeet@pobox.co m>
              > http://www.pobox.com/~skeet
              > If replying to the group, please do not mail me too[/color]


              Comment

              • Jon Skeet [C# MVP]

                #8
                Re: Two dimensional array initialization

                Daniel Billingsley <dbillingsley@N O.durcon.SPAAMM .com> wrote:[color=blue]
                > Hmm... I thought jagged simply meant an array of arrays... what did you mean
                > by the "normal sense" of jagged usage?[/color]

                I'd say that a jagged array of "X"s is an array of arrays of "X"s.
                [color=blue]
                > an array of ints is 1 dimensional, and not relevant to this discussion in my
                > mind.[/color]

                Ah, but it is in mine. Here's a jagged array of ints:

                int[][] x;

                That means a jagged array *of* arrays is similarly:

                Something[][][] x;

                That's a jagged array of (Something[]).

                Just

                Something[][] x;

                is a jagged array of (Something) instead.

                In this case we have:

                Something[][,];

                which is a one-dimensional array of (Something[,]), rather than a
                jagged array of Something[,]. It may or may not (see below) count as a
                jagged array of Something. Semi-jagged perhaps?
                [color=blue]
                > an array of arrays, or an array of arrays of arrays, or an array of 2D
                > arrays, are all "jagged" I would think by definition, no?[/color]

                Not sure. Possibly, possibly not. My main point was that it wasn't a
                jagged array of 2D arrays, but now we're getting into deeper water :)

                I keep flip-flopping on whether or not I'd consider it jagged in
                general. I think I probably would, on balance, contrary to my previous
                post.

                --
                Jon Skeet - <skeet@pobox.co m>
                Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

                If replying to the group, please do not mail me too

                Comment

                • Daniel Billingsley

                  #9
                  Re: Two dimensional array initialization

                  I'm sure where you landed when you flip-flopped, so I don't know if we're
                  still disagreeing. :)

                  "Jon Skeet [C# MVP]" <skeet@pobox.co m> wrote in message
                  news:MPG.19f0d4 f1f86a491798985 8@msnews.micros oft.com...[color=blue]
                  > Daniel Billingsley <dbillingsley@N O.durcon.SPAAMM .com> wrote:[color=green]
                  > > Hmm... I thought jagged simply meant an array of arrays... what did you[/color][/color]
                  mean[color=blue][color=green]
                  > > by the "normal sense" of jagged usage?[/color]
                  >
                  > I'd say that a jagged array of "X"s is an array of arrays of "X"s.[/color]

                  Yes I agree. MS defines a jagged array as "an array whose elements are
                  arrays." But there is no specification of what that second "of arrays"
                  looks like - 1D, 2D, 3D doesn't change the definition.

                  [color=blue]
                  > Something[][,];
                  >
                  > which is a one-dimensional array of (Something[,]), rather than a
                  > jagged array of Something[,]. It may or may not (see below) count as a
                  > jagged array of Something. Semi-jagged perhaps?[/color]

                  Is Something[,] not an array? Then that makes Something[][,] an
                  array of arrays, and therefore jagged by definition, just as MS explains a
                  bit further down on that same doc page:

                  "It is possible to mix jagged and multidimensiona l arrays. The following is
                  a declaration and initialization of a single-dimensional jagged array that
                  contains two-dimensional array elements of different sizes:

                  int [] [,] myJaggedArray = new int [] [3,] { new int [,] " blah blah blah
                  [color=blue]
                  >
                  > Not sure. Possibly, possibly not. My main point was that it wasn't a
                  > jagged array of 2D arrays, but now we're getting into deeper water :)[/color]

                  Sure looks to me like MS just said [][,] is a jagged array of 2D arrays.

                  I'm sorry, I'm not trying to be a smart alec proving you wrong per se.. it's
                  just that I find myself completely confused and wrong enough that when I
                  think I've got something nailed for once I've got to cling to it
                  desperately. LOL.


                  Comment

                  • Daniel Billingsley

                    #10
                    Re: Two dimensional array initialization

                    I meant I'm *NOT* sure, of course. I've been typing like a retard all day.
                    Need coffee.

                    "Daniel Billingsley" <dbillingsley@N O.durcon.SPAAMM .com> wrote in message
                    news:usg1G01jDH A.1408@TK2MSFTN GP11.phx.gbl...[color=blue]
                    > I'm sure where you landed when you flip-flopped, so I don't know if we're
                    > still disagreeing. :)
                    >[/color]


                    Comment

                    Working...