重复值n次 [英] Repeat values for n times
本文介绍了重复值n次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个值的集合
[1, 4, 23, 90]
,这些值应存储到重复 3
次且不重复的数组中使用 Linq
and these values should be stored into a array repeated 3
times without using Linq
[1, 1, 1, 4, 4, 4, 23, 23, 23, 90, 90, 90]
到目前为止我已经尝试过的
what I've tried so far
int[] collection = { 1, 4, 23, 90 };
int multiplier = 3;
int[] result = new int[collection.Length * multiplier];
for (int i = 0; i < collection.Length; i++)
for (int j = 0; j < multiplier; j++)
result[i + j] = collection[i];
但不知何故,只有$ 6
个字段数组已填充
but somehow only the first 6
fields of the array are filled
推荐答案
如果您不是正在寻找 Linq 解决方案,
If you are not looking for a Linq solution,
然后只需计算要放入结果
中的项目: i
次结果
的项目对应于 i /乘数
集合$ c $ s一个
then just compute which item to put into result
: i
-th result
's item corresponds to i / multiplier
collection
s one
int[] collection = new int[] { 0, 2, 25, 30 };
int multiplier = 3;
int[] result = new int[collection.Length * multiplier];
for (int i = 0; i < result.Length; i++)
result[i] = collection[i / multiplier];
这篇关于重复值n次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文