<分区>
如何将一个数组拆分为分别具有奇数和偶数索引的 2 个数组?例如
int[] a = new int[]{1, 3, 7, 8};
然后得到两个数组
a1: {1, 7}
a2: {3, 8}
标签 c# collections
<分区>
如何将一个数组拆分为分别具有奇数和偶数索引的 2 个数组?例如
int[] a = new int[]{1, 3, 7, 8};
然后得到两个数组
a1: {1, 7}
a2: {3, 8}
最佳答案
使用 Where 的重载很简单than 包含以下索引:
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();
关于c# - 如何将数组拆分为分别具有奇数和偶数索引的 2 个数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37382990/