Asp.net C # array traversal, sorting, element deletion, insertion, and random element

Source: Internet
Author: User

Asp tutorial. net c # array traversal, sorting, deleting elements, inserting, and random elements
Array Traversal

Short [] sts = {0, 100,200 };
For (int I = 0; I <sts. lenght; I ++)
{
If (sts [I]> 50)
{
.....
}
}


Random array 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;
}


 

View an array operation class

Using system;
Using system. collections. generic;
Using system. text;
Using system. text. regularexpressions;
Using system. windows. forms;
Namespace arr
{
/*
* Functions:
* Add a value to the array. You do not need to manually expand the array size.
* Add two Arrays
* Remove the specified items in the array.
* Remove repeated items in the array (either a number or a string. There are two types of algorithms for strings. You can compare them by yourself)
* Remove some of the same items from the array, which is not commonly used. It was previously written as needed and has been useless for a long time.
* Re-sort the Array (the number array can be from big to small or from small to big, and only the numbers and string arrays are sorted in descending order)
* Get the most repeated items in the array. Only the string array is used. If you need a number array, you can add it yourself. The algorithm is the same.
* Pstutorial: I am not an old fan of c #. If there is any mistake in the algorithm, please forgive me.
* Communication 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 items in the array
/// <Summary>
/// Remove the specified items in the array
/// </Summary>
/// <Param name = "arr"> A String Array </param>
/// <Param name = "index"> index item </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>
/// Remove the specified items in the array
/// </Summary>
/// <Param name = "arr"> an array of numbers </param>
/// <Param name = "index"> index item </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

# Adding region Arrays
/// <Summary>
/// Add an array
/// </Summary>
/// <Param name = "arrays"> a two-dimensional array composed 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 ++)
{
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 + ": Click 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 ++)
{
If (arrays [I]! = Null)
{
Arrays [I]. copyto (newarray, enditemnumber );
Enditemnumber + = arrays [I]. length;
}
}

Return newarray;
}
# Endregion

For example:

String aa = "12, 13, 16"; so how to make it one by 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 ('" descrithis.txt mail. value + "', '" + id + "', default )";
}
}
Dbcon. upconnection (txtsqlinsert); // This is a function that is commonly used to input an SQL statement.

/// <Summary>
/// Execute SQL statements and return nothing
/// </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.exe cutenonquery (); // execute the t-SQL statement and return the number of affected rows.
Con. close ();
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.