1

I have

string[] a = {"1","2","3","4","5"};

I want to create a second array and have it store *3 what array a has.

So the second array would look like:

string[] b = {"1","2","3","4","5","1","2","3","4","5","1","2","3","4","5"};

I was thinking of using Array.Copy, but is there another way?

6
  • 1
    What is wrong with using Array.Copy? Just copy the array to the three segments of the new array. Commented May 9, 2015 at 20:25
  • What have you tried and where were you stuck? What problem did you reach in order to start thinking you should explore a different way of doing that? Commented May 9, 2015 at 20:26
  • There is nothing wrong with it, but sometimes there are different ways of solving the same problem, so I was asking if there is another way to do it. Sometimes there is a function in MSDN that someone knows which does exactly what you are trying to do without implementing anything else, so I thought someone might know a method that replicates an array into another without any external logic or code. Commented May 9, 2015 at 20:27
  • Use a List<string> and AddRange Commented May 9, 2015 at 20:27
  • 1
    This question has merit, but it would be improved if you gave some desired characteristics for alternatives, rather than just seeking a list of equivalent approaches. (For example, you might want an alternative that's more flexible, more compact, more readable, or more performant.) Commented May 9, 2015 at 20:39

3 Answers 3

6

Here is a fun LINQ statement which gets your desired output:

b = Enumerable.Range(0, 3).SelectMany(p => a).ToArray();
Sign up to request clarification or add additional context in comments.

4 Comments

Its correct, but dirty. Sometimes it's better to write some more lines of code, but have better readability.
It's a little harder to read, especially for anyone not familiar with LINQ extension methods, but this would be a decent solution for replicating an array n number of times (i.e. if the value of "3" is not fixed). I'd probably wrap it in a convenience function to make its purpose a little more clear. :)
@PawelMaga Yep. I'd agree. However, the question was "Is there another way?". I'm simply giving an example.
Also, this is neither here nor there, but I wish the API declared InclusiveRange and ExclusiveRange functions, whose parameters were the range bounds. (start, count) just seems unintuitive. :P
0

The below sample performs worse than use of Array.Copy--it's more just playing with ways of accomplishing what you ask. The final result is obtained with a very simple looking call of string[] newArray = a.ArrayRepeat(3);

using System;
using System.Collections.Generic;
using System.Linq;

namespace ArrayRepeat
{
    public static class ArrayHelper
    {
        public static IEnumerable<T> Repeater<T>(this T[] a, int reps)
        {
            for (int i = 0; i < reps; i++)
            {
                foreach (var item in a)
                {
                    yield return item;
                }
            }
        }

        public static T[] ArrayRepeat<T>(this T[] a, int reps)
        {
            return a.Repeater<T>(reps).ToArray();
        }
    }
    class Program
    {

        static void Main(string[] args)
        {
            string[] a = { "1", "2", "3", "4", "5" };
            string[] newArray = a.ArrayRepeat(3);

            var pressKeyToExit = Console.ReadKey();
        }
    }
}

Comments

0

Here's another approach that is not hackish, seems clean, and is maybe even useful, although it still will not perform as well as Array.Copy:

using System;
using System.Linq;

namespace RepeatArray
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] a = { "1", "2", "3", "4", "5" };
            string[] newArray = Enumerable.Repeat(a, 3).SelectMany(x => x).ToArray();

            var pressKeyToExit = Console.ReadKey();
        }
    }
}

I should give props to @cmcquillan. I was trying to figure out how to get around the quirkiness of his SelectMany(p => x) which just discards p. The idea comes from him, however.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.