拆分数组的最佳方法

编程入门 行业动态 更新时间:2024-10-24 18:19:33
本文介绍了拆分数组的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

下午, 我需要找出将数组拆分为较小的块"的最佳方法是什么.

Afternoon, I need to find out what the best way to split an array into smaller "chunks" would be.

我要传递大约1200多个项目,并且需要将它们分成更容易处理的100个组,然后我需要将它们传递给处理.

I am passing over about 1200 items, and need to split these into easier to handle groups of 100, then i need to pass them over to processed.

有人可以提出一些建议吗?

Could any one please make some suggestions?

推荐答案

您可以使用LINQ按块大小对所有项目进行分组,然后再创建新的数组.

You can use LINQ to group all items by the chunk size and create new Arrays afterwards.

// build sample data with 1200 Strings string[] items = Enumerable.Range(1, 1200).Select(i => "Item" + i).ToArray(); // split on groups with each 100 items String[][] chunks = items .Select((s, i) => new { Value = s, Index = i }) .GroupBy(x => x.Index / 100) .Select(grp => grp.Select(x => x.Value).ToArray()) .ToArray(); for (int i = 0; i < chunks.Length; i++) { foreach (var item in chunks[i]) Console.WriteLine("chunk:{0} {1}", i, item); }

请注意,没有必要创建新的数组(需要CPU周期和内存).省略两个ToArrays时,也可​​以使用IEnumerable<IEnumerable<String>>.

Note that it's not necessary to create new arrays(needs cpu cycles and memory). You could also use the IEnumerable<IEnumerable<String>> when you omit the two ToArrays.

这是正在运行的代码: ideone/K7Hn2

Here's the running code: ideone/K7Hn2

更多推荐

拆分数组的最佳方法

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

发布评论

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

>www.elefans.com

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