有一组随机数需要循环放到数组中.. (这个随机数是五位以上的,随机数全都是唯一的)
我现在想做的是能不能使用什么记录的方式,使拿随机数中的任何一个数可以直接定位到存放随机数的数组下标上
用集合可以吗?

解决方案 »

  1.   

    使用List<int> _Num =new List<int>(); _Num.Add(1234234); 范行不可以吗?
      

  2.   

     可以
     Random rand = new Random();
                List<int> list = new List<int>();
                list.Add(rand.Next(11111, 99999));
         
      

  3.   

    通过泛型添加随机数
    public list<int> GetRandomArray(int Number,int minNum,int maxNum)
      {
       int j;
       List<int> b=new List<int>();
       Random r=new Random();
       for(j=0;j<Number;j++)
       {
        int i=r.Next(minNum,maxNum+1);
        int num=0;
        for(int k=0;k<j;k++)
        {
         if(b.Contains(i))
         {
          num=num+1;
         }
        }
        if(num==0 )
        {
         b.Add(i);
        }
        else
        {
         j=j-1;
        }
       }
       return b;
      } 
      

  4.   

    lz 等于是想用value来反找key吧
      

  5.   

    字典字典
    Dictionary,泛型
    key value队
      

  6.   

    用一个Dictionary<int, List<int>>对象保存随机数与数组下标的对应关系,前面的int为随机数,后面的List为该随机数对应的各个下标。每次向数组中存入数据时,先用TryGetValue获取该随机数对应的List,(如果返回False,则New一个新的List,Add到Dictionary中),然后将下标添加到List中。