如何在两个数组的数组分裂? [英] How to split array in two arrays?

查看:172
本文介绍了如何在两个数组的数组分裂?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数组,并希望它在分裂两种:

I have one array and want split it in to two:

现在:     [0,1,2,3,4,5,6,7,8,9]

Now:     [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

New_1: [0,2,4,6,8]

New_2: [1,3,5,7,9]

因此​​,采取的一个元素,并跳到下一个元素。
看起来容易,但我怎样才能用C#做呢?

So take the one element and skip the next element. Look easy but how can i do it with C#?

非常感谢

推荐答案

您可以使用LINQ,的 Enumerable.Where 并获得与元素数组具有的甚至的和的的索引。

You can use linq, Enumerable.Where and get the array with elements that have even and odd indexes.

var New_1 = arr.Where((c,i) => i % 2 == 0).ToArray();
var New_2 = arr.Where((c,i) => i % 2 != 0).ToArray();

您可以收集元素的索引和应用情况,以检查是否指数为奇数还是偶数,并根据获得的数组。

You can get the index of element of collection and apply condition to check if index is even or odd and get the arrays according.

Enumerable.Where方法(IEnumerable的,Func键)滤波器基于一个predicate值的序列。
  每一个元素的索引是在predicate函数的逻辑中使用。
  predicate重新$ P $的第一个参数psents测试的元素。该
  再$ P $第二个参数psents元素中的从零开始的索引
  源, MSDN

Enumerable.Where Method (IEnumerable, Func) filters a sequence of values based on a predicate. Each element's index is used in the logic of the predicate function. The first argument of predicate represents the element to test. The second argument represents the zero-based index of the element within source, msdn

这篇关于如何在两个数组的数组分裂?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆