Hello let me show you the problem with very simple explanation.
I have this;
int[] numbers1= { 1, 2, 3 };
and i need this;
int[] numbers2= { 1,1,1, 2,2,2, 3,3,3 };
how can i duplicate my values then use them in another list?
Hello let me show you the problem with very simple explanation.
I have this;
int[] numbers1= { 1, 2, 3 };
and i need this;
int[] numbers2= { 1,1,1, 2,2,2, 3,3,3 };
how can i duplicate my values then use them in another list?
Try:
using System.Linq;
namespace WebJob1
{
internal class Program
{
static void Main()
{
int[] numbers1 = { 1, 2, 3 };
var numbers2 = numbers1.SelectMany(x => Enumerable.Repeat(x,3)).ToArray();
}
}
}
You can do this with LINQ
var numbers2 = numbers1.SelectMany(x => new[]{x,x,x});
Live example: https://dotnetfiddle.net/FLAQIY
What about an algoritmic answer:
public class Program
{
public static void Main()
{
int[] numbers1= { 1, 2, 3 };
var numbers2 = ArrayMultiplier.MultiplyArray(numbers1, 3); //{ 1,1,1,2,2,2,3,3,3 }
}
public static class ArrayMultiplier{
public static int[] MultiplyArray(int[] array, int multiplicationCount){
int[] result = new int[array.Length*multiplicationCount];
for(int i =0; i<array.Length; i++){
for(int j = 0; j<multiplicationCount; j++){
result[i*multiplicationCount + j] = array[i];
}
}
return result;
}
}
}