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 + "\r\n";
}
}

private void button2_Click(object sender, EventArgs e)
{
String[] myArray = { "8", "one", "4", "0", "over", "the" };

foreach (string str in myArray)
textBox1.Text += str + "\r\n";

textBox1.Text += "\r\n";

Array.Sort(myArray);

foreach (string str in myArray)
textBox1.Text += str + "\r\n";
}

private void button3_Click(object sender, EventArgs e)
{
String[] myArray = { "8", "one", "4", "0", "over", "the" };

foreach (string str in myArray)
textBox1.Text += str + "\r\n";

textBox1.Text += "\r\n";

Array.Reverse(myArray);

foreach (string str in myArray)
textBox1.Text += str + "\r\n";
}

private void button4_Click(object sender, EventArgs e)
{
String[] myArray = { "one", "two", "three" };

foreach (string str in myArray)
textBox1.Text += str + "\r\n";

textBox1.Text += "\r\n";
Array.Resize(ref myArray, 5);

myArray[3] = "aaa";
myArray[4] = "bbb";

foreach (string str in myArray)
textBox1.Text += str + "\r\n";
}

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 + "\r\n";

textBox1.Text += "\r\n";

//要自己写一个SubStringis0000的函数,这是泛型编程
string[] subArray = Array.FindAll(dinosaurs,SubStringis0000);

foreach (string str in subArray)
textBox1.Text += str + "\r\n";


}

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 + "\r\n";

textBox1.Text += "\r\n";

string[] deststr = new string[2];
//Copy还有很多类型的参数,比如数组复制等。
Array.Copy(dinosaurs, 2, deststr, 0, 2);

foreach (string str in deststr)
textBox1.Text += str + "\r\n";
}

private void button7_Click(object sender, EventArgs e)
{
textBox1.Text = "";
}
}
}

你可能感兴趣的:(编程,C++,c,C#)