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 = "";
}
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值