c# 打乱数组
有时候得到了一个List,我想把它随机排列一下顺序。而且如果针对不同类型的List都能用,就要用到泛型。
其实思想很简单,就是从原List中每次随机取一项,添加到新的List中,并在原List中删除。这样重复,直到原List为空为止。
不过要注意,如果要保护原List不受变化,就必须先Copy一份List,再在Copy上进行操作
public static List<T> GetRandomList<T>(List<T> inputList)
{
//Copy to a array
T[] copyArray = new T[inputList.Count];
inputList.CopyTo(copyArray); //Add range
List<T> copyList = new List<T>();
copyList.AddRange(copyArray); //Set outputList and random
List<T> outputList = new List<T>();
Random rd = new Random(DateTime.Now.Millisecond); while (copyList.Count > 0)
{
//Select an index and item
int rdIndex = rd.Next(0, copyList.Count - 1);
T remove = copyList[rdIndex]; //remove it from copyList and add it to output
copyList.Remove(remove);
outputList.Add(remove);
}
return outputList;
} 可以这样
List<T> l = new List<T>();
l.Sort(delegate(T a, T b) { return (new Random()).Next(-1, 1); });
也可以用linq
List<T> l = new List<T>();
l = l.Select(a => new { a, newID = Guid.NewGuid() }).OrderBy(b => b.newID).Select(c=>c.a).ToList();
c# 打乱数组的更多相关文章
- [Swift]LeetCode384. 打乱数组 | Shuffle an Array
Shuffle a set of numbers without duplicates. Example: // Init an array with set 1, 2, and 3. int[] n ...
- 常用的sort打乱数组方法真的有用?
JavaScript 开发中有时会遇到要将一个数组随机排序(shuffle)的需求,一个常见的写法是这样: function shuffle(arr) { arr.sort(function () { ...
- ShuffleElements(随机打乱数组中的元素)
给定一个数组,随机打乱数组中的元素,题意很简单直接上代码: package Array; import java.util.Arrays; import java.util.Collections; ...
- 打乱数组——shuffle
在学习vue移动端音乐项目时,看到一个打乱数组函数,感觉很有意思就记录一下(意外发现:slice是个有趣的知识点) 原理:遍历数组,(let i = 0; i < _arr.length; i+ ...
- js打乱数组的实战应用
文章首发于: https://www.xiabingbao.com/post/javascript/js-random-array.html 在js中,能把数组随机打乱的方法有很多,每个方法都有自己的 ...
- 384 Shuffle an Array 打乱数组
打乱一个没有重复元素的数组.示例:// 以数字集合 1, 2 和 3 初始化数组.int[] nums = {1,2,3};Solution solution = new Solution(nums) ...
- LeetCode初级算法--设计问题01:Shuffle an Array (打乱数组)
LeetCode初级算法--设计问题01:Shuffle an Array (打乱数组) 搜索微信公众号:'AI-ming3526'或者'计算机视觉这件小事' 获取更多算法.机器学习干货 csdn:h ...
- Java实现 LeetCode 384 打乱数组
384. 打乱数组 打乱一个没有重复元素的数组. 示例: // 以数字集合 1, 2 和 3 初始化数组. int[] nums = {1,2,3}; Solution solution = new ...
- php保留键随机打乱数组顺序
最近遇到一个需求,把一个数组随机打乱顺序,我们可以用php的shuffle函数,但是这个函数会把数组的键清空建立新的键,那么我们若想保留键只需要利用shuffle函数再做一下处理就可以了.可以自定义一 ...
- c#数组乱序,打乱数组
按照random随机给出的index,进行两两交换,当然也存在与上一次一样的数组结果.官方还有一种ICompare的比较器,只是打乱顺序这个没用起来,不知道该怎么搞,╮(╯_╰)╭ public st ...
随机推荐
- php webservice
发请求客户端client.php <?php //需要到php.ini文件中打开extension=php_soap.dll try{ //wsdl方式调用web service //wsdl方 ...
- Orcle数据库 表的 内置函数 内链接 外连接 相关练习题
- CSS2样式中选择器的介绍
这里主要是对css2中的选择器进行了一下总结!
- android 命名 数组 所有国家 String[] COUNTRIES
static final String[] COUNTRIES = new String[] { "Afghanistan", "Albania", " ...
- 第十一章 GUI 上
第11章 GUI程序设计 11.1 JFC简介 JFC(Java Foundation Class) 作为CUI(Graphic User Interface)设计的基础.JFC包含AWT(Abst ...
- NPOI分层导出
using NPOI.HSSF.UserModel; using NPOI.POIFS.FileSystem; using org.in2bits.MyXls; using System; using ...
- HTML5离线缓存问题
HTML5离线缓存问题 1.应用程序缓存 什么是应用程序缓存(Application Cache)? HTML5 引入了应用程序缓存,这意味着 web 应用可进行缓存,并可在没有因特网连接时进行访问. ...
- iOS 使用Block进行逆传值
跟通知一样也是两个控制器,然后代码创建控件直接上代码 #import "ViewController.h" #import "TwoViewController.h&qu ...
- Java Web之会话管理二:Session
一.Session 在web开发中,服务器可以为每个yoghurt浏览器创建一个会话对象(Session)对象.注意:一个浏览器独占一个Session对象.因此,在需要保存用户数据时,服务器程序可以把 ...
- 浅析word-break work-wrap区别
word-break:[断词] 定义:规定自动换行的处理方法. 注:通过word-break使用,可以实现让浏览器在任意位置换行. 语法:word-break: normal|break-all| ...