1

I have an array

string[] AddrArray = new string[] { truncString }; 

which contains only one value <Addr>{Addr : n}</Addr>

how can I duplicate this value n number of times inside the same array. Thanks in advance

Ian
  • 30,182
  • 19
  • 69
  • 107
Architect
  • 39
  • 1
  • 5
  • 1
    Could you clarify your question? Right now it is unclear what exactly you'rу trying to do and what is your problem. how do you need to "duplicate" it? Should ut be a new array having N elements instead of one? Or should it be string enlarged and duplicated? – Andrey Korneyev Jan 22 '16 at 08:35
  • 2
    Add this item in a for loop to your array `n` times? – Soner Gönül Jan 22 '16 at 08:36
  • 2
    Possible duplicate of [How do I quicky fill an array with a specific value?](http://stackoverflow.com/questions/5089076/how-do-i-quicky-fill-an-array-with-a-specific-value) – Michael Jan 22 '16 at 08:46
  • Like I said I have only one array string[] AddrArray = new string[] { truncString }; holding only one value {Addr : n}. I want to duplicate this value like {Addr : n}{Addr : n}{Addr : n} n number of times – Architect Jan 22 '16 at 09:27

3 Answers3

4

Use Enumerable.Repeat in your array initialization may simplify your task:

string[] repeatedStrArray = Enumerable.Repeat(truncString, N).ToArray(); //specify N with the number of times you want
Ian
  • 30,182
  • 19
  • 69
  • 107
  • What if the array contains more than one item? Even if it works in this case, the question was how to repeat an array not a single string(which also would be a [duplicate](http://stackoverflow.com/questions/5089076/how-do-i-quicky-fill-an-array-with-a-specific-value)). – Tim Schmelter Jan 22 '16 at 08:50
  • @TimSchmelter Actually, since the question is not that clear, all of us, when answering assumes certain things. In my case, the assumption being truncString is a `string`. I can see the reason why you put `SelectMany` to flatten the result, just in case. I would say that it is also a valid option - more robust in that sense. – Ian Jan 22 '16 at 08:54
  • I just wondered why OP uses an array at all if a second string isn't possible. – Tim Schmelter Jan 22 '16 at 08:58
1

One way is to use Enumerable.Repeat to get n-arrays and SelectMany to flatten them to 1 array with n elements. This works even if the array contains more than one element:

AddrArra = Enumerable.Repeat(AddrArra, n).SelectMany(arr => arr).ToArray();

But i would prefer a custom extension method which is readable and more efficient:

public static IEnumerable<T> DuplicateItems<T>(this IEnumerable<T> items, int factor)
{
    if (items == null) throw new ArgumentNullException("items");
    if (factor <= 0) throw new ArgumentException("factor must be >= 1", "factor");
    if (factor == 1) return items;

    List<T> list = new List<T>();
    using (var enumerator = items.GetEnumerator())
    {
        for (int i = 0; i < factor; i++)
        {
            while (enumerator.MoveNext())
                list.Add(enumerator.Current);
            enumerator.Reset();
        }
    }
    return list;
}

Simple to use:

int n = 10;
AddrArra = AddrArra.DuplicateItems(n).ToArray();
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
0
 int repeateCount=5;
 string input = "<Addr>{Addr : n}</Addr> ";
 string[] AddrArray =Enumerable.Repeat(input, repeateCount).ToArray() ; 
sujith karivelil
  • 28,671
  • 6
  • 55
  • 88