list排序问题. 一个list里有数字 1, 2, 3我在读取这个list后, 第一次读是 1,2,3第二次读是2,1,3第三次读是3,1,2第四次读是3,2,1每次读取这个list都显示不同的排序. 应该怎么做呢? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 什么LIST?List<int>?每次读都不一样你想随机还是要一定规律? 不可能,肯定是你修改了这个list,否则次序不会改变, List本质上是数组,不会自己随便变更,应该是你在for循环内做了某些操作导致的 现在的需求是:一个list里有数字 1, 2, 3我在读取这个list后, 第一次读是 1,2,3第二次读是2,1,3第三次读是3,1,2第四次读是3,2,1每次读取这个list都显示不同的排序. 应该怎么实现呢? static void Main(string[] args) { List<int> list = new List<int> { 1, 2, 3 }; List<List<int>> result = new List<List<int>>(); Sort(list, 0, list.Count - 1, ref result); result.ForEach(x => { x.ForEach(y => Console.Write(y)); Console.WriteLine(); }); Console.ReadLine(); } public static void Sort(List<int> list, int i, int n, ref List<List<int>> result) { if (n == i) { List<int> temp = new List<int>(); foreach (int l in list) temp.Add(l); result.Add(temp); } else { for (int j = i; j <= n; j++) { Change(list, i, j); Sort(list, i + 1, n, ref result); Change(list, i, j); } } } public static void Change(List<int> list, int a, int b) { int temp = list[a]; list[a] = list[b]; list[b] = temp; }/*123132213231321312*/ list=list.OrderBy(l=>Guid.NewGuid()).ToList(); 请各位大侠看看,如何在SQL数据库中提取不同的二进制文件并显示在同一个richtextbox中 用C#语言编了一个菱形程序,不过有点问题。输出像个三角形,怎么改改? c# @代表什么呀? 使用SqlCommand对象的ExcuteNonQuery方法 如何设置打印WORD的纸张类型 关于事务的一个问题? asp中的 if then 等于c#中的哪个循环语句? 两个简单的问题 请问怎样把汉字等特殊字符串转变成为地址栏能传递的参数? VS2010调用水晶报表调试时报错 求一个加密解密算法,需要支持长字符串(100以上甚至更多) 检测到无法执行的代码
每次读都不一样你想随机还是要一定规律?
一个list里有数字 1, 2, 3
我在读取这个list后, 第一次读是 1,2,3
第二次读是2,1,3
第三次读是3,1,2
第四次读是3,2,1每次读取这个list都显示不同的排序. 应该怎么实现呢?
static void Main(string[] args)
{
List<int> list = new List<int> { 1, 2, 3 };
List<List<int>> result = new List<List<int>>();
Sort(list, 0, list.Count - 1, ref result);
result.ForEach(x => { x.ForEach(y => Console.Write(y)); Console.WriteLine(); });
Console.ReadLine();
}
public static void Sort(List<int> list, int i, int n, ref List<List<int>> result)
{
if (n == i)
{
List<int> temp = new List<int>();
foreach (int l in list)
temp.Add(l);
result.Add(temp);
}
else
{
for (int j = i; j <= n; j++)
{
Change(list, i, j);
Sort(list, i + 1, n, ref result);
Change(list, i, j);
}
}
}
public static void Change(List<int> list, int a, int b)
{
int temp = list[a];
list[a] = list[b];
list[b] = temp;
}
/*
123
132
213
231
321
312*/