重复值n次

编程入门 行业动态 更新时间:2024-10-24 19:23:56
本文介绍了重复值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,

插入重复值linq

然后只需计算要放入结果中的项目: i 次结果的项目对应于 i /乘数 集合

then just compute which item to put into result: i-th result's item corresponds to i / multiplier collections 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次

本文发布于:2023-11-30 20:18:18,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1651358.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!