在C#里,ArrayList是动态数组,可随时添加元素。添加元素常用Add和AddRange方法。 using System;using System.Collections;class Program { static void Main() { ArrayList list = new ArrayList(); list.Add(10); // 添加单个...
在C#中,删除ArrayList里的元素可用Remove、RemoveAt和RemoveRange方法。 using System;using System.Collections;class Program { static void Main() { ArrayList list = new ArrayList(); list.Add(10); list.Add(20); list.Add(30); list...
C#提供了方便的方法对数组排序。可使用Array.Sort()方法对数组升序排序。若要降序排序,可先升序排序,再反转数组。 // 升序排序示例int[] arr = { 3, 1, 2 };Array.Sort(arr);// 运行结果:arr包含...
合并数组时,可创建新数组,大小为原数组大小之和,再复制元素。拆分数组,需确定拆分位置,创建新数组存放拆分后的元素。 // 合并数组示例int[] arr1 = { 1, 2 };int[] arr2 = { 3, 4 };int...
在C#里,ArrayList是常用的动态数组,可动态增减元素。遍历ArrayList能访问每个元素。 // 示例1:用for循环遍历ArrayListusing System;using System.Collections;class Program { static void Main() { ArrayList list...
在C#里,查找ArrayList元素可借助IndexOf和Contains方法。 // 示例1:用IndexOf方法查找元素位置using System;using System.Collections;class Program { static void Main() { ArrayList list = new ArrayList(); list.Add(1); li...
C# 中的哈希表(Hashtable)是一种数据结构,它存储键值对。哈希表使用哈希函数将键映射到存储桶,以便快速查找值。 // 引入命名空间using System.Collections;class Program { static void Main() {...
可以通过键来访问哈希表中的值。如果键不存在,访问时会返回 null。 using System.Collections;class Program { static void Main() { Hashtable ht = new Hashtable(); ht.Add("key1", "value1"); // 通过键访问值,运行...
在C#里,哈希表(Hashtable)可用来存储键值对。添加元素时,需用Add方法。 // 引入命名空间using System;using System.Collections;class Program{ static void Main() { // 创建哈希表 Hashtable ht = new Hashtabl...
在C#中,可通过Remove方法从哈希表里删除元素。 // 引入命名空间using System;using System.Collections;class Program{ static void Main() { // 创建哈希表 Hashtable ht = new Hashtable(); ht.Add("key1", "value1"); ht.Ad...