Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Dont understand arrays

Dont understand arrays

Scheduled Pinned Locked Moved C#
6 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    netJP12L
    wrote on last edited by
    #1

    Hi guys, I know how single dimensional arrays works but I don't understand at all these GetUpperBound and GetLowerBound methods for multi dimensional arrays. I know somebody would have a very simple way to educate me so i could have a broad smile on my face. Thanks string[,] myarray = new string[,] { { "01", "Honda" }, { "02", "GM" }, { "04","Ford"} }; myarray.GetUpperBound(0);

    M S A 3 Replies Last reply
    0
    • N netJP12L

      Hi guys, I know how single dimensional arrays works but I don't understand at all these GetUpperBound and GetLowerBound methods for multi dimensional arrays. I know somebody would have a very simple way to educate me so i could have a broad smile on my face. Thanks string[,] myarray = new string[,] { { "01", "Honda" }, { "02", "GM" }, { "04","Ford"} }; myarray.GetUpperBound(0);

      M Offline
      M Offline
      Migounette
      wrote on last edited by
      #2

      Look at this msdn article: [^]

      1 Reply Last reply
      0
      • N netJP12L

        Hi guys, I know how single dimensional arrays works but I don't understand at all these GetUpperBound and GetLowerBound methods for multi dimensional arrays. I know somebody would have a very simple way to educate me so i could have a broad smile on my face. Thanks string[,] myarray = new string[,] { { "01", "Honda" }, { "02", "GM" }, { "04","Ford"} }; myarray.GetUpperBound(0);

        S Offline
        S Offline
        Sir Dot Net
        wrote on last edited by
        #3

        Every array has at least 1 dimension, for instance: string[] x1 = new string[] {...} // is 1 dimensional string[,] x2 = new string[,] {...} // is 2 dimensional string[,,] x3 = new string[,,] {...} //is 3 dimensional Each dimension has upper and lower bounds, which in most cases is easiest to think about as a capacity, since the lower bound is almost always 0. The GetUpper and LowerBounds functions help you get the bounds (lowest possible index to highest possible index) for the given dimension, so... Here we are creating a 1 dimentional array with a capacity of 5 elements. string[] x1 = new string[5]; now we pass the target dimension (0 index based) to the function... GetLowerBounds(0) would say 0. GetUpperBounds(0) would say 4. Why does upper bounds say 4? Because the bounds functions are returning 0 based indexes, thus, the capacity is 5, and the 5 indexes of each 'space' are 0,1,2,3, and 4. If we were to do this: GetLowerBounds(1) or GetUpperBounds(1) we would get an exception, because our array has only 1 dimension (thus we would pass index 0). More reading here: http://msdn.microsoft.com/en-us/library/system.array.getlowerbound.aspx[^] http://www.java2s.com/Tutorial/CSharp/0220__Data-Structure/ArrayGetLowerBoundGetUpperBound.htm[^]

        1 Reply Last reply
        0
        • N netJP12L

          Hi guys, I know how single dimensional arrays works but I don't understand at all these GetUpperBound and GetLowerBound methods for multi dimensional arrays. I know somebody would have a very simple way to educate me so i could have a broad smile on my face. Thanks string[,] myarray = new string[,] { { "01", "Honda" }, { "02", "GM" }, { "04","Ford"} }; myarray.GetUpperBound(0);

          A Offline
          A Offline
          AspDotNetDev
          wrote on last edited by
          #4

          When you use an initializer list, the outermost array is the first dimension and the innermost array is the last dimension. So, myarray[0,0] is "01" and myArray[0,1] is "Honda" (notice that I didn't change the index for the first dimension). Another way of seeing it is that the first index of the first dimension points to {"01", "Honda"}... you have to specify an index into the second dimension to get either "01" or "Honda". In the example you gave, the first dimension (GetUpperBound(0)) has an upper bound of 2 (because there are 3 items, the third item would be referenced by index 2). The second dimension (GetUpperBound(1)) has an upper bound of 1 (because there are 2 items in each subdimension, the second items are referenced by index 1). If that doesn't make sense, play with it a bit and you'll eventually get it via osmosis. By the way, in C# the lower bound should always be 0.

          [Forum Guidelines]

          L 1 Reply Last reply
          0
          • A AspDotNetDev

            When you use an initializer list, the outermost array is the first dimension and the innermost array is the last dimension. So, myarray[0,0] is "01" and myArray[0,1] is "Honda" (notice that I didn't change the index for the first dimension). Another way of seeing it is that the first index of the first dimension points to {"01", "Honda"}... you have to specify an index into the second dimension to get either "01" or "Honda". In the example you gave, the first dimension (GetUpperBound(0)) has an upper bound of 2 (because there are 3 items, the third item would be referenced by index 2). The second dimension (GetUpperBound(1)) has an upper bound of 1 (because there are 2 items in each subdimension, the second items are referenced by index 1). If that doesn't make sense, play with it a bit and you'll eventually get it via osmosis. By the way, in C# the lower bound should always be 0.

            [Forum Guidelines]

            L Offline
            L Offline
            Luc Pattyn
            wrote on last edited by
            #5

            aspdotnetdev wrote:

            you'll eventually get it via osmosis.

            osmosis? that's what doesn't happen when you put your C# book under your pillow and hope it will "sink in" while you're asleep. :)

            Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


            Getting an article published on CodeProject should be easier and faster for Bronze and Silver authors.


            A 1 Reply Last reply
            0
            • L Luc Pattyn

              aspdotnetdev wrote:

              you'll eventually get it via osmosis.

              osmosis? that's what doesn't happen when you put your C# book under your pillow and hope it will "sink in" while you're asleep. :)

              Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


              Getting an article published on CodeProject should be easier and faster for Bronze and Silver authors.


              A Offline
              A Offline
              AspDotNetDev
              wrote on last edited by
              #6

              Osmosis is when you hear Arnold say "My CPU is a neural net processor -- a learning computer" and only understand what that means years later when you understand the underlying concepts from different perspectives. :)

              [Forum Guidelines]

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups