您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關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#如何實現數組操作”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。