ASP tutorials. NET C # array traversal, sorting, delete elements, inserts, random elements
Array traversal
short[] sts={0,1,100,200};
for (int i=0;i<sts.lenght;i++)
{
if (STS[I]>50)
{
.....
}
}
Array random elements
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;
}
Look at an array action class
using System;
Using System.Collections.Generic;
Using System.Text;
Using System.Text.RegularExpressions;
Using System.Windows.Forms;
Namespace arr
{
/*
* Implementation function:
* Add a value to the array without the need to manually extend the array size
* Two arrays added
* Remove the specified item from the array
* Remove duplicates in an array (either a number or a string is OK.) The string has two algorithms, each colleague compares merits and demerits by oneself
* Move the group of the same part of the same item, estimated not commonly used in the past when they need to write up, a long time no use.
* Array reordering (numeric arrays can be large to small or small to large, with numbers and string arrays in descending order only)
* Get the most repeated items in the array, only a string array, the number of arrays if you need, you can add, the same algorithm.
* PS Tutorial: I am not a veteran of C #, if the algorithm has errors in the place, but also please forgive.
* Exchange qq:9729052
*/
Class Array
{
#region Add a value to the array
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 to remove the specified item from the array
<summary>
Removes the specified item from the array
</summary>
<param name= "arr" > An array of Strings </param>
<param name= "index" > Index entry </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>
Removes the specified item from the array
</summary>
<param name= "Arr" > a numeric array </param>
<param name= "index" > Index entry </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
Add #region Array
<summary>
Array addition
</summary>
<param name= "Arrays" > two-dimensional array of one-dimensional arrays </param>
<returns></returns>
public static string[] Arrayadd (string[][] arrays)
{
* Example:
* 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++)
& nbsp; {
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 + ": come Here");
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++)
& nbsp; {
if (arrays[i]!= null)
{
Arrays[i].copyto (NewArray, Enditemnumber);
Enditemnumber + = arrays[i].length;
}
}
return newarray;
}
#endregion
For example:
String aa= "12,13,14,15,16"; So how do I get it to insert a database tutorial? The idea is as follows:
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);//This is a function that is commonly used to pass in an SQL statement.
///<summary>
///Execute SQL statements without returning
///</summary>
/// <param name= "Sqlstr" ></param>
public static void Upconnection (String sqlstr)
& nbsp {
SqlConnection con=new SqlConnection (Constr);
SqlCommand cmd=new SqlCommand (sqlstr,con);
Con.open () ;
cmd.executenonquery ();//execute T-SQL statements and return the number of rows affected
con.close ();
}