最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
asp.net C#数组遍历、排序、删除元素、插入、随机元素
时间:2011-01-17 编辑:简简单单 来源:一聚教程网
asp教程.net c#数组遍历、排序、删除元素、插入、随机元素
数组遍历
short[] sts={0,1,100,200};
for(int i=0;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 去除数组中指定的项
///
/// 去除数组中指定的项
///
/// 一个字符串数组
/// 索引项
///
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;
}
}///
/// 去除数组中指定的项
///
/// 一个数字数组
/// 索引项
///
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 数组相加
///
/// 数组相加
///
/// 由一维数组组成的二维数组
///
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语句很常用的函数了。///
/// 执行sql语句什么都不返回
///
///
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();
}
相关文章
- C#复制数组的两种方式及效率比较解读 10-24
- ASP.NET Identity用法解析 10-24
- ASP.NET MVC使用Identity增删改查用户介绍 10-24
- C语言中atoi函数模拟实现介绍 10-18
- .Net反向代理组件Yarp用法介绍 10-10
- .NET使用YARP通过编码方式配置域名转发实现反向代理教程 10-10