0

How can I append all the string arr into one arr within a for loop?

string[] finalArray = new string[5];
for (int i = 0; i < finalArray.Length; i++)
{                            
     string[] array1 = GenRandArray1(); 
     string[] array2 = GenRandArray2();
     string[] array3 = GenRandArray3();
     string[] array4 = GenRandArray4();

     // I want to combine all the string array to my finalArray.
     finalArray[i] = array1 + array2 + array3 + array4
}

Since I make loop 5 times and store in finalArray[i], so the sample output will be something like this.

"12345","17896","685","984","063","991","6","9","3","6","68","20","69","52"
"43256","24356","765","345","347","983","2","1","0","5","90","34","12","76"
"76324","98754","542","625","267","865","3","2","1","8","23","43","76","86"
"00982","16864","537","847","249","136","1","0","9","3","65","80","23","17"
"12569","98754","658","345","646","999","5","9","6","3","94","63","15","47"
Melvin
  • 31
  • 5
  • Does this help? https://stackoverflow.com/questions/59217/merging-two-arrays-in-net – Ryan Thomas Jun 30 '21 at 07:07
  • 1
    How should the end result looks like? I am confused with `finalArray`, it will contains 5 identical results. – Sinatr Jun 30 '21 at 07:08
  • @RyanThomas, thanks for the comment but seems not work for my case, since my finalArray contains 5 identical result – Melvin Jun 30 '21 at 07:12
  • @Sinatr for further handle – Melvin Jun 30 '21 at 07:12
  • 1
    This question needs more work, you need to explain what the results need to look like, what the input is, and why only a for loop works for you – TheGeneral Jun 30 '21 at 07:35
  • @TheGeneral kindly check the new edit, hope its clear enough for you :) – Melvin Jun 30 '21 at 07:53
  • What's missing is the description of what the ` GenRandArray#()` methods do. From their names, I assume that they just return a string array of random length containing random numbers (in string form). Is that correct? – Matthew Watson Jun 30 '21 at 08:09
  • @MatthewWatson correct! – Melvin Jun 30 '21 at 08:10
  • Are you allowed to use a `List` and it's `ToArray` method? Then you can make your life easy(no LINQ inside). – Tim Schmelter Jun 30 '21 at 08:11
  • @TimSchmelter but I have to loop thru 5 times to store 5 different set of return result... So for my case I should I do this – Melvin Jun 30 '21 at 08:12
  • One further question: You declared your `finalArray` as `string[]` but your requirements seem to indicate that it should be `string[][]`, i.e. each entry in it is an array of strings. Is that correct? If you look at your sample output, you can see that each row is a list of strings, so each row is either `string[]` or `List`. – Matthew Watson Jun 30 '21 at 08:13
  • You can do this without a loop, [just using Linq, like this](https://dotnetfiddle.net/FqTivu). – Matthew Watson Jun 30 '21 at 08:34

2 Answers2

1

Do you mean concat all the arrays or a different algorithm? If you just want to concat all the arrays it just like below.

string[] finalArray = new string[5];
string[] array1 = new[] { "1", "2", "4" };
string[] array2 = new[] { "9", "3", "6" };
string[] array3 = new[] { "4", "0", "0" };
string[] array4 = new[] { "7", "8", "2" };

finalArray = finalArray.Concat(array1).Concat(array2).Concat(array3).Concat(array4).ToArray();
omerbguclu
  • 69
  • 6
  • Your way was not allow to do with forloop and `finalArray[i]` – Melvin Jun 30 '21 at 07:22
  • @Melvin why you want to use a for loop. Explain us exactly what are you want ? – omerbguclu Jun 30 '21 at 07:25
  • Actually no any special reason, just my project needed, of course my real code wont be like hardcode string array, it actually is call another method and it will doing some random generate and finally will return back a list of string array. – Melvin Jun 30 '21 at 07:30
0

From your question it is not clear if you really want to concat your arrays five times, but let's assume that that is the case in your simplified code. In addition, as you remarked, you will be returning a list of arrays

Then:


    List<string[]> finalArrayList = new List<string[]>();

    for(int i=0; i<5; i++)
    {
        string[] array1 = new[] { "1", "2", "4" };
        string[] array2 = new[] { "9", "3", "6" };
        string[] array3 = new[] { "4", "0", "0" };
        string[] array4 = new[] { "7", "8", "2" };

        List<string> ConcatList = new List<string>(array1);
        ConcatList.AddRange(array2);
        ConcatList.AddRange(array3);
        ConcatList.AddRange(array4);

        finalArrayList.Add(ConcatList.ToArray());
    }

Amo Robb
  • 810
  • 5
  • 11