0

How can I properly do so that a check is performed for each string?

My code looks like :

string[,] screeny_baza = new string[300, 300];
for (int i = 0; i < 299; i++)
{
    try
    {
        string nazwa_screna_pokolei = screeny_baza[0,i]
    }
    catch { };
}

As i see i want to do just it one by one. Is it possible to do it faster omitting values ​​that do not exist or have not been declared? They are just null i think.

I have dimension that looks like to 300/300 .

   X0 X1 X2
Y0 00 10 20
Y1 01 11 21
Y2 02 12 22

And want to save just string to each of dimesnion for example

string [00] = "bird";
string [01] = "bird2";

and later need to get this values in loop ( omitting values ​​that do not exist or have not been declared)

Thanks for help.

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
adam
  • 179
  • 2
  • 12
  • 1
    Two dimensions will use a loop nested inside of another. And then of course you can check whether it is null before you act. – Broots Waymb Oct 12 '18 at 13:22
  • maybe there is better way to declare variables like it to faster get values? When i set only value for [150,150] it's gonna check every dimension , is it possible check only declared values? Here 150,150 – adam Oct 12 '18 at 13:29
  • Do not worry about speed right now, **seriously**. For loops are fast. If you're experiencing noticeable slowdown, it's probably something else. *Do not* worry about premature optimization! – Broots Waymb Oct 12 '18 at 13:32
  • I don't if the question is ["Foreach on 2d array"](https://stackoverflow.com/questions/2834233), or [through 2d array](https://stackoverflow.com/questions/8184306/iterate-through-2-dimensional-array-c-sharp), or [checking for null](https://stackoverflow.com/questions/14204782/best-c-sharp-method-for-checking-for-a-null-string-value?noredirect=1&lq=1) because of the try catch – Drag and Drop Oct 12 '18 at 13:37
  • See if you can work with jagged arrays instead. Then each row can have a `foreach` loop. – John Alexiou Oct 12 '18 at 14:10
  • @adam just a side-note: You do realize, your loop as is would only go to index 298 instead of 299? In most cases it is safe to say "better use the length property of whatever you are iterating over" - unless you have a good reason not to. – Fildor Oct 12 '18 at 14:16

5 Answers5

2

I don't know about foreach loops on multi dimensional arrays but you can always do this:

string[,] screeny_baza = new string[300, 300];
for (int x = 0; x < screeny_baza.GetLength(0); x++)
{
    for (int y = 0; y < screeny_baza.GetLength(1); y++)
    {
        try
        {
            string nazwa_screna_pokolei = string.empty;
            if (screeny_baza[x, y] != null)
                nazwa_screna_pokolei = screeny_baza[x, y];
        }
        catch { };
    }
}
DaanV
  • 339
  • 1
  • 6
1

You can foreach on a 2d array. You can even LinQ Where to filter it.

var table = new string[20, 20];
table[0, 0] = "Foo";
table[0, 1] = "Bar";

foreach (var value in table.Cast<string>().Where(x =>!string.IsNullOrEmpty(x))) {
    Console.WriteLine(value);
}
Drag and Drop
  • 2,672
  • 3
  • 25
  • 37
1

Actually your try-catch block will not raise any exception because when you construct the array:

string[,] screeny_baza = new string[300, 300];

you can always index it as long as the indexes are in range; so the statement:

string nazwa_screna_pokolei = screeny_baza[0,i];

will execute without error. Just nazwa_screna_pokolei will be null;

Also if speed is concerned, a nested for-loop is much faster than LinQ. at least for this simple check. for example:

var list = screeny_baza.Cast<string>().Where(x => !string.IsNullOrEmpty(x)).ToList();

will take about 10 milliseconds, but

for (int i = 0; i < 300; i++)
{
    for (int j = 0; j < 300; j++)
    {
        if (string.IsNullOrEmpty(screeny_baza[i,j]))
        {
            continue;
        }
            list.Add(screeny_baza[i, j]);
    }
}

will take only 1 millisecond.

roozbeh S
  • 1,084
  • 1
  • 9
  • 16
0

For storing you would use row and column indices like:

screeny_baza[0,0] = "bird";
screeny_baza[0,1] = "bird";

For looping the values you use GetLength (though you know the dimensions as constant, this is a more flexible way):

for (int row = 0; row < screeny_baza.GetLength(0); row++) {
    for (int col = 0; col < screeny_baza.GetLength(1); col++) {
        if (!string.IsNullOrEmpty(screeny_baza[row,col])) // if there is a value
        Console.WriteLine($"Value at {row},{col} is {screeny_baza[row,col]}");
    }
}
Cetin Basoz
  • 22,495
  • 3
  • 31
  • 39
0

My simple helper for such cases

    private static void forEachCell(int size, Action<int, int> action)
    {
        for (int i = 0; i < size; i++)
        {
            for (int j = 0; j < size; j++)
            {
                action(j, i);
            }
        }
    }

It can be modified to use different sizes. Usage example:

        double totalProbability = 0;

        forEachCell(chessboardSize, (row, col) =>
            totalProbability += boardSnapshots[movesAmount][row, col]);

        return totalProbability;
Lev
  • 811
  • 12
  • 13