C# List中随机获取N个字符_.NET_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > .NET > C# List中随机获取N个字符

C# List中随机获取N个字符

 2015/4/2 16:04:41  Jetlian  程序员俱乐部  我要评论(0)
  • 摘要:1staticvoidMain(string[]args)2{3List<string>strList=newList<string>();4for(inti=1;i<=50;i++)5{6strList.Add("string"+i);7}8intnumber=10;9if(strList.Count<number)10{11number=strList.Count;12}1314intcount=0;15List<string>
  • 标签:C# list
 1  static void Main(string[] args)
 2         {
 3             List<string> strList = new List<string>();
 4             for (int i = 1; i <= 50; i++)
 5             {
 6                 strList.Add("string "+i);
 7             }
 8             int number = 10;
 9             if (strList.Count < number)
10             {
11                 number = strList.Count;
12             }
13 
14             int count=0;
15             List<string> resultList = new List<string>();
16             for (int i = 1; i <= number; i++)
17             {
18                 int rang = strList.Count - i;
19                 int index = new Random().Next(0, rang);
20                 //Console.WriteLine("ramg:" + rang + "  strList[" + index + "]:" + strList[index]);
21                 resultList.Add(strList[index]);
22                 strList[index] = strList[rang];
23                 count++;
24             }
25             Console.WriteLine("---------COUNT: " + count);
26             foreach (var result in resultList)
27             {
28                 Console.WriteLine(result);
29             }
30 
31             Console.ReadLine();
32 
33         }

 

发表评论
用户名: 匿名