如何将一个数组拆分为分别具有奇数和偶数索引的2个数组? [英] How to split an array to 2 arrays with odd and even indices respectively?

查看:109
本文介绍了如何将一个数组拆分为分别具有奇数和偶数索引的2个数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将一个数组拆分为分别具有奇数和偶数索引的2个数组?例如,

How to split an array to 2 arrays with odd and even indices respectively? For example

int[] a = new int[]{1, 3, 7, 8};

然后得到两个数组


a1:{1,7}

a2:{3,8}

a1: {1, 7}
a2: {3, 8}


推荐答案

使用其中包含以下索引:


根据谓词过滤一系列值。每个元素的
索引都在谓词函数的逻辑中使用。

Filters a sequence of values based on a predicate. Each element's index is used in the logic of the predicate function.



int[] a = new int[] { 1, 3, 7, 8 };

int[] aEven = a.Where((x, i) => i % 2 == 0).ToArray();
int[] aOdd = a.Where((x, i) => i % 2 != 0).ToArray();

这篇关于如何将一个数组拆分为分别具有奇数和偶数索引的2个数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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