zoukankan      html  css  js  c++  java
  • 在数组中随机插入数字且不重复

    产生一个int数组,长度为100,并向其中随机插入1-100,并且不能重复。(两种方法)

    1,

         List<int> myList = new List<int>();

                Random ran = new Random();

                while (myList.Count<100)

                {

                    int num = ran.Next(1, 101);

                    if (!myList.Contains(num))

                    {

                        myList.Add(num);

                    }

                }

                foreach (int item in myList)

                {

                    Console.WriteLine(item);

                }

                Console.WriteLine(myList.Count);

                Console.ReadLine();

       2,

           HashSet<int> myList = new HashSet<int>();

                Random ran = new Random();

                while (myList.Count<100)

                {

                    int num = ran.Next(1, 101);

                    myList.Add(num);

                }

                foreach (int item in myList)

                {

                    Console.WriteLine(item);

                }

                Console.WriteLine(myList.Count);

                Console.ReadLine();

  • 相关阅读:
    JDK和TOMCAT环境变量配置
    MEF(Managed Extensibility Framework )的入门介绍
    这样的数据导出你知道?
    ListBox实现拖拽排序功能
    各种技术资源汇总
    大话数据结构-排序
    大话数据结构-查找
    CheckListBox的实现方式分析
    listbox里面添加WrapPanel ,支持自适应换行
    RESTful 接口实现简明指南
  • 原文地址:https://www.cnblogs.com/wangxlei/p/5991325.html
Copyright © 2011-2022 走看看