Dont understand arrays
-
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);
-
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);
Look at this msdn article: [^]
-
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);
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[^]
-
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);
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.
-
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.
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]
-
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]
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. :)