首页 > 学院 > 开发设计 > 正文

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

2019-11-17 02:28:26
字体:
来源:转载
供稿:网友

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

 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         }


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表