优点:内存中是连续的存储单元,且数据类型一致,索引速度快;
缺点:数组过长,容易造成内存溢出;插入数据很麻烦;
(2)、数组一维、二维、三维初始化如下:
string[] ListA = new string[] { "S", "M", "L", "XL", "XXL" };
int[,] A1 = new int[3, 3] { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
int[,,] A3 = new int[3, 4, 5] {
{{ 1,2,3,4,5}, { 6,7,8,9,10}, {1,2,3,4,5 } , { 6,7,8,9,10}},
{{ 1,2,3,4,5}, { 6,7,8,9,10}, {1,2,3,4,5 } , { 6,7,8,9,10}},
{{ 1,2,3,4,5}, { 6,7,8,9,10}, {1,2,3,4,5 } , { 6,7,8,9,10}}
};
优点:可以动态扩充及收缩长度,且可存储不同类型数据;
缺点:在获取数据量,因数据类型不同,容易产生成类型不匹配转换错误;装箱与拆 箱过程,带来很大性能损耗;
(装箱:将指定的数据类转换为object对象;拆箱:将object对象转换为指定对象类型;)
(2)链表用法:
ArrayList listA = new ArrayList();
listA.Add(123);
listA.Add("ABC");
listA.Insert(1, "ERP");
listA.RemoveAt(0);
foreach (var item in listA)
{
}
3、列表(List)
(1)优缺点:
优点:兼具数组特点数据类型是一致的,且可以动态扩充容量;提供了Sort、Find等 实用方法;克服了ArrayList装箱与拆箱过程产生的性能损耗;
缺点:使用列表可能分配比实际需要更多的空间造成内存浪费,列表在插入删除操作 需要移动元素加大系统开销;
(2)列表常规用法:
List<string> StrList = new List<string> { "A", "B", "C", "D", "E", "F", "G" };
List<int> DataList = new List<int> {1, 2 3,4, 5, 6, 7,8, 9 };
string[] AryStr = new string[] { "A", "B", "C", "D", "E", "F", "G" };
List<string> temp = Ary.ToList();
string str = String.Join(",", temp.ToArray());
List<string> TempList = new List<string>();
TempList = TempList.OrderBy(x => x.Length).ThenBy(x => x).ToList();
TempList = TempList.OrderByDescending(x => x.Length).ThenBy(x => x).ToList();
List<string> NewList = TempList.Distinct().ToList();
List<int> listA = new List<int> { 1, 2, 3, 5, 7, 9 };
List<int> listB = new List<int> { 13, 4, 17, 29, 2 };
listA.AddRange(listB);
List<int> ResultA = listA.Union(listB).ToList<int>();
List<int> ResultB = listA.Concat(listB).ToList<int>();
listA.BinarySearch(1);
bool equal = listA.SequenceEqual(listB);
(3)列表嵌套用法:(多维列表用法)
public class DataList
{
public int IndexNo { get; set; }
public string Type { get; set; }
public List<double> temp = new List<double>();
}
List<double> tempA = new List<double>();
List<DataList> dataOjb = new List<DataList>();
DataList nd = new DataList();
nd.IndexNo = 0;
nd.temp = tempA;
dataOjb.Add(nd);
var temp1 = dataOjb.GroupBy(x => x.Type).Select(x => new DataList { Type = x.Key }).ToList();
var temp2 = dataOjb.GroupBy(x => new { x.Type, x.IndexNo }).ToList();
优点:极快查找、键值对结构、自动管理容量;
缺点:内存开销大,需要维护哈希表和冲突处理结构,元素没有特定顺序,哈希冲突 会影响性能,不支持高效的范围查询;
(2)哈希表用法:
Hashtable hastb = new Hashtable();
hastb.Add("Id", "1000");
hastb.Add("Name", "zhangsan");
foreach (DictionaryEntry item in hastb)
{
Console.WriteLine(item.Key + " " + item.Value);
}
ArrayList akeys = new ArrayList(hastb.Keys);
akeys.Sort();
foreach (string item in akeys)
{
}
阅读原文
该文章在 2025/7/9 9:15:27 编辑过