I have a vector: int[,] leg = new int[100, 2];
and I put some numbers in it. Is there a function beside v.Length
that shows how many numbers I put in my vector?(v.Length
shows me that I have 200 numbers which is wrong because I put in just a few)
Asked
Active
Viewed 327 times
3

Victor
- 53
- 6
-
3Why do you want another function when you have one? – Shaharyar Jun 12 '16 at 16:49
-
7Or may be you're asking about how to get length of a dimension in a 2D array? So here it is: http://stackoverflow.com/questions/4106369/how-do-i-find-the-size-of-a-2d-array – Shaharyar Jun 12 '16 at 16:53
-
The accepted answer and the comment made to it make me think that this was incorrectly closed as duplicate. The answers on the linked question do not solve OP's problem. Voting to reopen. – bmm6o Jun 13 '16 at 15:52
-
1+1 He explicitly wrote "how many … I put". Marking that as duplicate of a Q about size, seems to be a misunderstanding of the Q, even his Q is quiete clear. – Amin Negm-Awad Jun 13 '16 at 19:24
1 Answers
3
What you have is a 2-dimensional array. The Length
property reports its size, which is fixed over its lifetime. When you create an array, the members are default initialized; in the case of an int array everything is set to 0. What you seem to be asking about is how many elements you have written to. This is not tracked anywhere. You could either count every time you assign to an element or count the non-zero elements. Note that these measure different things and could give different answers.

bmm6o
- 6,187
- 3
- 28
- 55