我已经到处寻找如何在C#中为Windows Phone 7打乱/随机化一个字符串列表。我仍然是一个初学者,你可以这样说,所以这可能是我的出路联盟,但我正在写一个简单的应用程序,这是它的基础。我有我需要洗牌并输出到文本块的字符串列表。我查阅了一些代码,但我知道我错了。有什么建议么?C#中的字符串清单在Windows Phone 7
0
A
回答
3
Fisher-Yates-Durstenfeld shuffle是一种易于实施的成熟技术。这是一个扩展方法,可以在任何IList<T>
上执行就地洗牌。
(这应该很容易,如果你决定要离开原来的清单完整,并返回一个新的洗牌列表,而不是,或act on IEnumerable<T>
sequences,点菜LINQ适应。)
var list = new List<string> { "the", "quick", "brown", "fox" };
list.ShuffleInPlace();
// ...
public static class ListExtensions
{
public static void ShuffleInPlace<T>(this IList<T> source)
{
source.ShuffleInPlace(new Random());
}
public static void ShuffleInPlace<T>(this IList<T> source, Random rng)
{
if (source == null) throw new ArgumentNullException("source");
if (rng == null) throw new ArgumentNullException("rng");
for (int i = 0; i < source.Count - 1; i++)
{
int j = rng.Next(i, source.Count);
T temp = source[j];
source[j] = source[i];
source[i] = temp;
}
}
}
相关问题
- 1. Windows Phone 7的文件清单IsolatedStorage
- 2. 在Windows Phone 7(C#)
- 3. 使用查询字符串来获取参数c#windows phone 7
- 4. 无法在Windows Phone 7中解析字符串DateTime
- 5. Windows Phone中的XAML字符串格式
- 6. 如何解析Windows Phone 7中的xml字符串?
- 7. 不能在Windows Phone的本地化字符串7
- 8. 在一个字符串数组导入XML - Windows Phone 7的
- 9. 获得在C#中的Windows Phone 7
- 10. 在windows phone 7中
- 11. Windows Phone 7 SDK C或C++
- 12. 如何将字符串转换为JSON在C#中的Windows Phone
- 13. 使用Windows Phone 7中的待办事项清单
- 14. 在windows phone 7中使用C dll
- 15. Xml串行器Windows Phone 7
- 16. AppResources中的C#/ Windows Phone转义字符
- 17. LongListSelector中的Windows Phone 7或Windows Phone 8
- 18. 如何在Windows Phone中输入大写字符串TextBox Wpf c#
- 19. 使用C#和Windows Phone 7
- 20. Windows Phone 7 - iCal Generator - C#
- 21. 在windows phone 7模拟器中打字
- 22. Windows Phone 7的
- 23. c#windows phone 7中的列表框
- 24. 在Windows Phone上解压缩字符串
- 25. 如何检查Windows Phone 7中的字符串数组中是否存在字符串?
- 26. Windows Phone 7中的列表框菜单
- 27. Windows Phone 7下载纯HTML作为字符串?
- 28. Windows Phone 7将资源字符串值设置为.xml
- 29. Windows Phone 7不读荷兰本地化字符串
- 30. ProgressBar在Windows Phone 7中?
HTTP: //stackoverflow.com/search?q=%5Bc%23%5D+shuffle – dtb 2011-04-07 23:21:44
如果你不需要随机混排,一个简单的选择是返回“strings.OrderBy(s => s.GetHashCode() );” ;) – 2011-04-07 23:43:01
可能重复的[C#:是使用随机和OrderBy一个很好的洗牌算法?](http://stackoverflow.com/questions/1287567/c-is-using-random-and-orderby-a-good-shuffle -algorithm) – Jon 2011-04-08 00:49:16