asp.net C#数组遍历、排序、删除元素、插入、随机元素

asp教程.net c#数组遍历、排序、删除元素、插入、随机元素
数组遍历

short[] sts={0,1,100,200};
for(int i=0;i<sts.lenght;i++)
{
  if(sts[i]>50)
 {
  .....
  }
}

数组随机元素

public  hashtable  noorder(int count)
        {
            arraylist mylist = new arraylist();
            hashtable hash = new hashtable();
            random r=new random ();
            for (int i = 0; i < count; i++)
            {
                mylist.add(i);
            }
            int listcount=mylist.count;
            for (int i = 0; i < listcount; i++)
            {
               int rand= r.next(mylist.count);
               hash.add(mylist[rand],i );
               mylist.removeat(rand);
            }
            return hash;
        }  

 

看一个数组操作类

using system;
using system.collections.generic;
using system.text;
using system.text.regularexpressions;
using system.windows.forms;
namespace arr
{
    /*
     * 实现功能:
     * 向数组中添加一个值,不需再手动扩展数组大小
     * 两个数组相加
     * 去除数组中指定的项
     * 去除数组中重复的项(数字或字符串都行。字符串有两种算法,各位同仁自行比较优劣)
     * 移除数组中部分相同的项,估计不常用,以前自己需要时写上去的,很久没用了。
     * 对数组重新排序(数字数组可由大到小或由小到大,带数字和字符串数组仅进行了降序排列)
     * 获取数组内重复最多的项,仅做了string数组,数字数组如果大家需要,可自行添加,算法一样。
     * ps教程:本人对c#不算老鸟,如果算法中有失误的地方,还请原谅。
     * 交流qq:9729052
    */
    class array
    {
        #region 向数组添加一个值
        public static string[] arrayadditem(string[] arr, string item)
        {
            string[] _arr = new string[arr.length + 1];
            arr.copyto(_arr, 0);
            _arr[arr.length] = item;
            return _arr;
        }

        public static string[][] arrayadditem(string[][] motherarray, string[] arr)
        {
            string[][] _arr = new string[motherarray.length + 1][];
            motherarray.copyto(_arr, 0);
            _arr[motherarray.length] = arr;
            return _arr;
        }
        #endregion

        #region 去除数组中指定的项
        /// <summary>
        /// 去除数组中指定的项
        /// </summary>
        /// <param name="arr">一个字符串数组</param>
        /// <param name="index">索引项</param>
        /// <returns></returns>
        public static string[] removeat(string[] arr, int index)
        {
            if (index >= arr.length || index < 0)
            {
                return arr;
            }
            else
            {
                string[] newarr = new string[arr.length - 1];
                array.copy(arr, 0, newarr, 0, index);
                array.copy(arr, index + 1, newarr, index, newarr.length - index);
                return newarr;
            }
        }

        /// <summary>
        /// 去除数组中指定的项
        /// </summary>
        /// <param name="arr">一个数字数组</param>
        /// <param name="index">索引项</param>
        /// <returns></returns>
        public static int[] removeat(int[] arr, int index)
        {
            if (index >= arr.length || index < 0)
            {
                return arr;
            }
            else
            {
                int[] newarr = new int[arr.length - 1];
                array.copy(arr, 0, newarr, 0, index);
                array.copy(arr, index + 1, newarr, index, newarr.length - index);
                return newarr;
            }
        }
        #endregion

        #region 数组相加
        /// <summary>
        /// 数组相加
        /// </summary>
        /// <param name="arrays">由一维数组组成的二维数组</param>
        /// <returns></returns>
        public static string[] arrayadd(string[][] arrays)
        {
            /*例:
             * string[] array1={ };
             *string[] array2={"1","2","3"};
             *string[] array3={"ab","cd","ef"};
             *string[][] arrays={array1,array2,array3};
             *string[] newarray=array.arrayadd(arrays);
            */
            int itemsnumber = 0;
            for (int i = 0; i < arrays.length; i++)
            {
                if (arrays[i] == null)
                    continue;
                else
                    itemsnumber += arrays[i].length;
            }
            int enditemnumber = 0;

            string[] newarray = new string[itemsnumber];

            for (int i = 0; i < arrays.length; i++)
            {
                if (arrays[i] != null)
                {
                    arrays[i].copyto(newarray, enditemnumber);
                    enditemnumber += arrays[i].length;
                }
            }

            return newarray;
        }

        public static int[] arrayadd(int[][] arrays)
        {
            int itemsnumber = 0;
            //system.windows.forms.messagebox.show(arrays.length + ":走到这里了");
            for (int i = 0; i < arrays.length; i++)
            {
                if (arrays[i] == null)
                    continue;
                else
                    itemsnumber += arrays[i].length;
            }
            int enditemnumber = 0;

            int[] newarray = new int[itemsnumber];

            for (int i = 0; i < arrays.length; i++)
            {
                if (arrays[i] != null)
                {
                    arrays[i].copyto(newarray, enditemnumber);
                    enditemnumber += arrays[i].length;
                }
            }

            return newarray;
        }
        #endregion

例如:

string aa=“12,13,14,15,16”;那么怎么让它一个一个的插入数据库教程呢?其思路如下:
string[] comids=aa.split(',');
    foreach(string id in comids)
    {
     if(id!="")
     {
      txtsqlinsert +="insert into comidlist(uemail,comids,sendtime) values('"+this.txtmail.value+"','"+id+"',default)";
     }
    }
    dbcon.upconnection(txtsqlinsert);//这个就是一个传入一个sql语句很常用的函数了。

/// <summary>
   /// 执行sql语句什么都不返回
   /// </summary>
   /// <param name="sqlstr"></param>
   public static void upconnection(string sqlstr)
   {
    sqlconnection con=new sqlconnection(constr);
  
    sqlcommand cmd=new sqlcommand(sqlstr,con);
    con.open();
    cmd.executenonquery();//执行t-sql语句,并返回受影响的行数
    con.close();
   }

时间: 2024-08-29 07:44:52

asp.net C#数组遍历、排序、删除元素、插入、随机元素的相关文章

class-编写一个使用类模板对数组进行排序、查找和求元素和的程序。

问题描述 编写一个使用类模板对数组进行排序.查找和求元素和的程序. 设计一个类模板templateclass Array,用于对T类型的数组进行排序.查找和求元素和,然后由此产生模板类Array和Array. 解决方案 http://www.warting.com/program/201109/33601.html 第四题 解决方案二: 编写一个使用类模板对数组进行排序,查找和求元素和的程序.

编程-数组类模板Array,类中包括对数组进行排序、查找和求元素和 然后由此产生模板类Array&amp;amp;lt;Box&amp;amp;gt;

问题描述 数组类模板Array,类中包括对数组进行排序.查找和求元素和 然后由此产生模板类Array<Box> #include using namespace std;class Box{private: int a b c;public: int V; Box(int chint kint g) { a = ch; b = k; c = g; V = a*b*c; } bool operator <(Box &one) { int temp = V - one.V; if (

JavaScript数组对象实现增加一个返回随机元素的方法_javascript技巧

本文实例讲述了JavaScript数组对象实现增加一个返回随机元素的方法.分享给大家供大家参考.具体如下: 核心特性: 概率随机.顺序随机.随机冒泡 本方法 来自个人手写 JavaScript 的实践,只涉及 JavaScript 1.5(ECMAscript 3 国际标准)语言本身,在所有 JS 引擎实现中通用~ 为Array对象新增random方法: (function () { function Random_SN(iArray) { return Math.floor(Math.rand

JavaScript数组对象实现增加一个返回随机元素的方法

为Array对象新增random方法: (function () {   function Random_SN(iArray) {     return Math.floor(Math.random() * iArray.length);   }   function Probability_Random(iArray) {     var Random_Int;     if (iArray.Random_SN === undefined)       iArray.Random_SN = -

PHP入门教程之数组用法汇总(创建,删除,遍历,排序等)_php技巧

本文实例总结了PHP数组用法.分享给大家供大家参考,具体如下: Demo1.php <?php //创建一个数组变量 $userNames = array('张三','李四','王五'); //将这个数组打印出来 // echo $userNames;//Array // $userName = '张三'; // echo $userName;//张三 //如果你想打印出这个数组的某一个元素 //那你必须找到这个元素的下标,键(key) //0,1,2 //echo $userNames[2];

PHP学习笔记之数组值及数组遍历和排序

数组值的操作 1. 值的析取 PHP中,用list来析取数组中的值,如list($a, $b) = $array.如果list中的值多于数组个数,list中多余的值会设为NULL.也可以用逗号来跳过数组中的值,如list($a, ,$b) = $array. 2.划分数组 如果想取得子数组,可以用array_slice(array, offset, length);来取得.它返回一个新的下标从0开始的数组.如果原数组的下标是字符串,好像是没有什么意义的,最好不要用,可以用array_splice

Java中Collection遍历中删除、合并元素

我的分词结果链表需要合并连续的数字和日期,所以需要熟悉一下Java Collection在遍历的过程中同时删除.合并元素的小trick.自己试验了一下,活用listIterator的previous()和next()方法就可以达到目的. 遍历中删除 数据集 List<Integer> integerList = new LinkedList<Integer>(); for (int i = 1; i <= 10; ++i) {     integerList.add(i);

js数组依据下标删除元素

 1.创建数组   代码如下: var array = new Array(); var array = new Array(size);//指定数组的长度 var array = new Array(item1,item2--itemN);//创建数组并赋值   2.取值.赋值   代码如下: var item = array[index];//获取指定元素的值 array[index] = value;//为指定元素赋值   3.添加新元素    代码如下: array.push(item1

C#集合遍历时删除和增加元素的方法_C#教程

本文实例讲述了C#集合遍历时删除和增加元素的方法.分享给大家供大家参考,具体如下: 大多数时候,遍历集合元素的时候并不需要对元素进行增加或者删除操作,但有些时候则需要,比如,如果集合中盛放的元素是社会上所有的人,那么有人死亡则元素删除,有人出生则是集合元素的增加.对于这种情况,遍历不能按照原来那种方式去做了,而且C#中的集合对于这类有增删动作的遍历,也不支持foreach循环. 有三种办法可以解决这一问题. 第一种方法:使用C#的LinkedList<>双链表.我原来设想,把原来链表需要删除的