本文向大家介绍C#数组操作方法,可能好多人还不了解C#数组操作,没有关系,看完本文你肯定有不少收获,希望本文能教会你更多东西。

专注于为中小企业提供做网站、成都网站制作服务,电脑端+手机端+微信端的三站合一,更高效的管理,为中小企业兴和免费做网站提供优质的服务。我们立足成都,凝聚了一批互联网行业人才,有力地推动了1000+企业的稳健成长,帮助中小企业通过网站建设实现规模扩充和转变。
数组是相同类型的对象的集合。数组具有相同数据类型的项的有序集合。要访问数组中的某个项,需要同时使用数组名称及该项与数组起点之间的偏移量。由于数组几乎可以为任意长度,因此可以使用数组存储数千乃至数百万个对象,但必须在创建数组时就确定其大小。数组中的每项都按索引进行访问,索引是一个数字,指示对象在数组中的存储位置或槽。
按顺序演示了以下功能:
◆动态创建数组
◆数组快速排序
◆反转数组元素
◆动态改变数组大小
◆检索数组中元素
◆复制数组中多个元素
- 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 = "";
 - }
 - }
 - }
 
【编辑推荐】