ASP tutorial. NET C # array traversal, sorting, deleting elements, inserting, 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 manipulation class
using System;
Using System.Collections.Generic;
Using System.Text;
Using System.Text.RegularExpressions;
Using System.Windows.Forms;
Namespace arr
{
/*
* Implementation Features:
* Add a value to the array without manually expanding the array size
* Two arrays added
* Remove the specified item from the array
* Remove duplicate entries in the array (numbers or strings are rows.) There are two kinds of algorithms for strings, so colleagues compare their merits
* To remove some of the same items in the divisor group, it is estimated that it is not used, and it is useless to write it in the past.
* Reorder Arrays (numbers array can be large to small or small to large, with numbers and string arrays only in descending order)
* Gets the most repeated items in the array, only the string array, the number array if you need, you can add, the same algorithm.
* PS Tutorial: I do not count as a C # veteran, if the algorithm has errors in the place, please also forgive.
* AC 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 Remove the specified item from the array
<summary>
Removes the specified item from the array
</summary>
<param name= "Arr" > a string array </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 digital 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
#region Array Additions
<summary>
Array additions
</summary>
<param name= "Arrays" > two-dimensional array consisting 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 + ": Walk 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 you make it a one-insert 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 very common function for passing in a SQL statement.
<summary>
Execute SQL statement does not return anything
</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 ();//execute T-SQL statement and return the number of rows affected
Con.close ();
}
Go: ASP tutorial. NET C # array traversal, sort, delete element, insert, random element array traversal