【发布时间】:2012-05-28 20:31:46
【问题描述】:
编辑:我尝试了 Take/Skip 方法,但出现以下错误:
Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<string>' to
'string[]'. An explicit conversion exists (are you missing a cast?)
我不知道自己做错了什么,因为我复制了 Saeed 的代码。
我有一个字符串数组(包含 20 到 300 个项目),我想将它分成 2 个单独的数组,从第一个数组的中间开始。
我知道如何使用 for 循环来做到这一点,但我想知道是否有更快/更好的方法来做到这一点。我还需要能够正确拆分数组,即使它有奇数个项目,例如:
string[] words = {"apple", "orange", "banana", "pear", "lemon"};
string[] firstarray, secondarray;
SplitArray(words, out firstarray, out secondarray); // Or some other function
// firstarray has the first 3 of the items from words, 'apple', 'orange' and 'banana'
// secondarray has the other 2, 'pear' and 'lemon'
【问题讨论】:
-
为了解决你的演员问题,在 Take 和 skip 方法之后添加
.ToArray()