How to implement Array Operation in C #
This article is about how C # implements array operations. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
An array is a collection of objects of the same type. An ordered collection of items with the same data type in an array. To access an item in an array, you need to use both the array name and the offset between the item and the beginning of the array. Because arrays can be of almost any length, you can use arrays to store thousands or even millions of objects, but you must determine the size of the array when you create it. Each item in the array is accessed by index, which is a number indicating where or where the object is stored in the array.
The following features are demonstrated in order:
◆ dynamically creates an array
Quick sort of ◆ array
◆ reverses array elements
◆ dynamically resizes the array
◆ retrieves elements in an array
◆ copies multiple elements in an array
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 strin mystrlist) {textBox1.Text + = str + "\ r\ n" } private void button2_Click (object sender, EventArgs e) {String [] myArray = {"8", "one", "4", "0", "over", "the"}; foreach (string strin myArray) textBox1.Text + = str + "\ r\ n"; textBox1.Text + = "\ r\ n"; Array.Sort (myArray); foreach (string strin myArray) textBox1.Text + = str + "\ r\ n" } private void button3_Click (object sender, EventArgs e) {String [] myArray = {"8", "one", "4", "0", "over", "the"}; foreach (string strin myArray) textBox1.Text + = str + "\ r\ n"; textBox1.Text + = "\ r\ n"; Array.Reverse (myArray); foreach (string strin myArray) textBox1.Text + = str + "\ r\ n" } private void button4_Click (object sender, EventArgs e) {String [] myArray = {"one", "two", "three"}; foreach (string strin myArray) textBox1.Text + = str + "\ r\ n"; textBox1.Text + = "\ r\ n"; Array.Resize (ref myArray, 5); myArray [3] = "aaa"; myArray [4] = "bbb"; foreach (string strin 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 strin dinosaurs) textBox1.Text + = str + "\ r\ n"; textBox1.Text + = "\ r\ n"; / / to write a function of SubStringis0000 yourself, which is generic programming string [] subArray = Array.FindAll (dinosaurs,SubStringis0000) Foreach (string strin 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 strin dinosaurs) textBox1.Text + = str + "\ r\ n"; textBox1.Text + = "\ r\ n"; string [] deststr = new string [2]; / / Copy also has many types of parameters, such as array replication. Array.Copy (dinosaurs, 2, deststr, 0,2); foreach (string strin deststr) textBox1.Text + = str + "\ r\ n";} private void button7_Click (object sender, EventArgs e) {textBox1.Text = ";} Thank you for reading! This is the end of this article on "how to implement array operations in C#". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!