c#数组操作实例,为大家按顺序演示如下的功能:
1、动态创建数组
2、数组快速排序
3、反转数组元素
4、动态改变数组大小
5、检索数组中元素
6、复制数组中多个元素
编辑推荐:c#数组基础知识
复制代码 代码示例:
namespace StringDemo  
{  
public partial class Form1 : Form  
{  
public Form1()  
{  
InitializeComponent();  
}  
 
private void button1_Click(object sender, EventArgs e)  
{  
System.Collections.ArrayList mystrlist = new System.Collections.ArrayList();  
 
mystrlist.Add("aaaaaaaa");  
mystrlist.Add("bbbbbbbb");  
mystrlist.Add("cccccccc");  
mystrlist.Add("dddddddd");  
 
foreach (string str in mystrlist)  
{  
textBox1.Text += str + "rn";  
}  
}  
 
private void button2_Click(object sender, EventArgs e)  
{  
String[] myArray = { "8", "one", "4", "0", "over", "the" };  
 
foreach (string str in myArray)  
textBox1.Text += str + "rn";  
 
textBox1.Text += "rn";  
 
Array.Sort(myArray);  
 
foreach (string str in myArray)  
textBox1.Text += str + "rn";  
}  
 
private void button3_Click(object sender, EventArgs e)  
{  
String[] myArray = { "8", "one", "4", "0", "over", "the" };  
 
foreach (string str in myArray)  
textBox1.Text += str + "rn";  
 
textBox1.Text += "rn";  
 
Array.Reverse(myArray);  
 
foreach (string str in myArray)  
textBox1.Text += str + "rn";  
}  
 
private void button4_Click(object sender, EventArgs e)  
{  
String[] myArray = { "one", "two", "three" };  
 
foreach (string str in myArray)  
textBox1.Text += str + "rn";  
 
textBox1.Text += "rn";  
Array.Resize(ref myArray, 5);  
 
myArray[3] = "aaa";  
myArray[4] = "bbb";  
 
foreach (string str in myArray)  
textBox1.Text += str + "rn";  
}  
 
private void button5_Click(object sender, EventArgs e)  
{  
string[] dinosaurs = { "Compsog0000nathus",  
"Amargasaurus", "Ovira0000ptor","Veloc0000iraptor",  
"Deinonychus","Dilop0000hosaurus","Gallimimus",  
"Triceratops"};  
 
foreach (string str in dinosaurs)  
textBox1.Text += str + "rn";  
 
textBox1.Text += "rn";  
 
//要写一个SubStringis0000的函数,泛型编程  
string[] subArray = Array.FindAll(dinosaurs,SubStringis0000);  
 
foreach (string str in subArray)  
textBox1.Text += str + "rn"; 
}  
 
private static bool SubStringis0000(string str)  
{  
if(str.Contains ("0000"))  
return true ;  
else  
return false ;  
}  
 
private void button6_Click(object sender, EventArgs e)  
{  
string[] dinosaurs = { "Compsog0000nathus",  
"Amargasaurus", "Ovira0000ptor","Veloc0000iraptor",  
"Deinonychus","Dilop0000hosaurus","Gallimimus",  
"Triceratops"};  
 
foreach (string str in dinosaurs)  
textBox1.Text += str + "rn";  
 
textBox1.Text += "rn";  
 
string[] deststr = new string[2];  
//Copy还有很多类型的参数,比如数组复制等。  
Array.Copy(dinosaurs, 2, deststr, 0, 2);  
 
foreach (string str in deststr)  
textBox1.Text += str + "rn";  
}  
 
private void button7_Click(object sender, EventArgs e)  
{  
textBox1.Text = "";  
}  
}  
}