-2

So I am assigning int[,] matrix = new int[1, 2];. But while debugging I see that my matrix has [0,0] [0,1].(slots) Why is that? I have 2 slots for values? Shouldn't it be only 1? (Don't tell me I have to use a one dimensional array, this is only an example). If I want to declare a 3 by 2 matrix for example (only 3 possible rows) [0, 0], [0, 1], [0, 2] how should I do it?

john
  • 41
  • 1
  • 5
  • You should have [0,0] and [0,1] as valid indices. Is that what you meant? – itsme86 Jun 12 '18 at 17:06
  • @itsme86 yea just edited – john Jun 12 '18 at 17:06
  • Why are you expecting only 1? Even a one-dimensional array like `new int[2]` gives you 2 slots (0 and 1). – itsme86 Jun 12 '18 at 17:07
  • 1
    What is 1 x 2 ? – paparazzo Jun 12 '18 at 17:08
  • @itsme86 the two slots come from the "1" right? – john Jun 12 '18 at 17:09
  • The two slots come from the 2 in the 2nd dimension. If you had `new int[1,1]` you would end up with a single usable slot [0,0]. – itsme86 Jun 12 '18 at 17:10
  • @itsme86 I'm such an ... I was wondering what I was mistaking... so the first dimension is basically the columns and the second is the rows right? Please reply the question so I can vote – john Jun 12 '18 at 17:14
  • @john I update my anwser so now its anwser your update question – Darem Jun 12 '18 at 17:15
  • Here's [more reading](https://stackoverflow.com/questions/12567329/multidimensional-array-vs) – Sach Jun 12 '18 at 17:16
  • @itsme86 I am more used to working with jagged arrays... So which is the column and which is the row in the multidimensional array? – john Jun 12 '18 at 17:20
  • You can do it whichever way you want, but for performance reasons (CPU cache hits), you want to iterate over higher dimensions more rapidly than lower dimensions. For instance, if you're iterating over rows and for each row you iterate over the columns, you'll want the rows to be the first dimension and the columns the second. If you have `int[3, 3]`, you want to access them [0,0] [0,1] [0,2] [1,0] [1,1], etc. Hopefully that makes sense. [This answer](https://stackoverflow.com/a/11148619/1141432) really nails it. – itsme86 Jun 12 '18 at 17:23

2 Answers2

1

A Array start always with Index 0

So

[0,0] = Dimension 1 Value 1 [0,1] = Dimension 1 Value 2

Edit:

If you want 3 Values in 1 Dimension you have to do

int [,] matrix = new int [1,3];

I Hope it helps you?

Darem
  • 1,689
  • 1
  • 17
  • 37
0

I guess you are confusing somehow.

Apparently a 1 x 2 matrix has 2 slots.

If you only need 1 slot, it's 1 x 1 matrix

Example:

enter image description here

Hope it helps

Tom Tran
  • 41
  • 5