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.

6
  • 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. Commented Oct 12, 2018 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 Commented Oct 12, 2018 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! Commented Oct 12, 2018 at 13:32
  • I don't if the question is "Foreach on 2d array", or through 2d array, or checking for null because of the try catch Commented Oct 12, 2018 at 13:37
  • See if you can work with jagged arrays instead. Then each row can have a foreach loop. Commented Oct 12, 2018 at 14:10

5 Answers 5

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 { };
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

@DragandDrop I don't seem to be able to make the connection. You can what ?
@ fildor he said "I dont know a bout for ach on multidim". Link is how to foreach. Missing a part of the comment with [foreach like this](link)
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);
}

Comments

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.

Comments

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]}");
    }
}

Comments

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;

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.