C#如何实现数组操作

发布时间:2021-10-21 14:27:23 作者:小新
来源:亿速云 阅读:104

这篇文章给大家分享的是有关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#如何实现数组操作”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!

推荐阅读:
  1. PHP:数组操作函数array_count_values()的实现
  2. JavaScript常用数组操作有哪些

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

上一篇:如何使用Python库中操作系统级别模块Psutil

下一篇:如何理解栈在括号匹配和表达式求值中的应用

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》