淺析C#數(shù)組操作方法
本文向大家介紹C#數(shù)組操作方法,可能好多人還不了解C#數(shù)組操作,沒有關(guān)系,看完本文你肯定有不少收獲,希望本文能教會(huì)你更多東西。
數(shù)組是相同類型的對(duì)象的集合。數(shù)組具有相同數(shù)據(jù)類型的項(xiàng)的有序集合。要訪問數(shù)組中的某個(gè)項(xiàng),需要同時(shí)使用數(shù)組名稱及該項(xiàng)與數(shù)組起點(diǎn)之間的偏移量。由于數(shù)組幾乎可以為任意長(zhǎng)度,因此可以使用數(shù)組存儲(chǔ)數(shù)千乃至數(shù)百萬個(gè)對(duì)象,但必須在創(chuàng)建數(shù)組時(shí)就確定其大小。數(shù)組中的每項(xiàng)都按索引進(jìn)行訪問,索引是一個(gè)數(shù)字,指示對(duì)象在數(shù)組中的存儲(chǔ)位置或槽。
按順序演示了以下功能:
◆動(dòng)態(tài)創(chuàng)建數(shù)組
◆數(shù)組快速排序
◆反轉(zhuǎn)數(shù)組元素
◆動(dòng)態(tài)改變數(shù)組大小
◆檢索數(shù)組中元素
◆復(fù)制數(shù)組中多個(gè)元素
- 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";
 - //要自己寫一個(gè)SubStringis0000的函數(shù),這是泛型編程
 - 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還有很多類型的參數(shù),比如數(shù)組復(fù)制等。
 - 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 = "";
 - }
 - }
 - }
 
【編輯推薦】















 
 
 
 
 
 
 