重复n次值
我有一组价值观
[1, 4, 23, 90]
并且这些值应该在不使用 Linq的 情况下重复存储到数组中3
次
[1, 1, 1, 4, 4, 4, 23, 23, 23, 90, 90, 90]
到目前为止我尝试过的
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
字段
如果您不是在寻找Linq解决方案,
插入重复值linq
然后只计算要放入result
的项目: i
-th的项目对应于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];