Concatenate arrays accumulating content in C #

2

Gentlemen, the question is of the C # programming language, my question is: how can I put all the items in array B within each item in array A and the result of that will be stored in array C. Thanks.

Thank you in advance ...

public void TesteUniaoArray3()

{
    string[] one = new string[] { "XXXXX-X-XXX", "YYYYY-Y-YYY" };
    string[] two = new string[] { "1", "2", "3", "4", "5" };
    string[] three;

    three = new string[one.Length * two.Length];

    for (int i = 0; i <= one.Length; i++)
       for (int j = 0; j <= two.Length; j++)
            for (int idx = j; idx <= two.Length; idx++)
                try
                {
                    three[idx] = one[i] + "-" + two[j++];
                }

                catch (Exception)
                {
                    idx = three.Length;
                }
            }
        }
    }
}
    
asked by anonymous 07.04.2018 / 16:05

1 answer

2

You've used an extra loop, which has created you a problem. It's simpler solving it this way:

string[] one = new string[] { "XXXXX-X-XXX", "YYYYY-Y-YYY" };
string[] two = new string[] { "1", "2", "3", "4", "5" };
string[] three;

three = new string[one.Length * two.Length];

int idx = 0;

for (int i = 0; i < one.Length; i++)
    for (int j = 0; j < two.Length; j++)
    {
        three[idx] = one[i] + "-" + two[j];
        idx++;
    }

//foreach(var i in three)
//  Console.WriteLine(i);

The result would be according to what you expect, such as can check in dotnetFiddle

    
07.04.2018 / 18:39