A
A
Alexey Smirnov2016-03-03 02:49:07
.NET
Alexey Smirnov, 2016-03-03 02:49:07

How to calculate to what value the array is filled for each of the dimensions?

Hello.
There is some two-dimensional array, for example:
string[,] dataArray = new string[10, 10];
And this array is filled with some string values, but not completely, but only up to some values. For example, by dimension (dimension) - 0 to 4, and by dimension (dimension) - 1 to 5.
How to calculate to what value the array is filled for each of the dimensions?
PS Familiar with the method:

.GetUpperBound(0)
.GetUpperBound(1)

but unfortunately this method considers the size of the entire array for each of the dimensions, not paying attention to the fullness of the array.
PPS Writing to the array occurred sequentially and from the very beginning of the array

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexey Kulakov, 2016-03-03
@ERAFY

Go through the array and check what is filled there. There are two options here, run from the beginning and look for the first empty one or run from the end and look for the first filled one. choose your own strategy.
There is also the question of how the recording goes - randomly, in any cell, so to speak, or sequentially.
If randomly, then most likely you need to take the viewing option from the end. If sequentially, then it is generally possible to do without a bypass, remembering the maximum index (s) on which (th) the record was made.

M
Michael, 2016-03-08
@Madfisht3

for(int i = 0; i < 10; i++)
{
  int counter = 0;
  for(int j = 0; dataArray[i,j] != default(string) && j < 10; j++)
    counter = j;
  Console.WriteLine("{0} length {1}", i, counter);
}

More or less like this.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question