優點:內存中是連續的存儲單元,且數據類型一致,索引速度快;
缺點:數組過長,容易造成內存溢出;插入數據很麻煩;
(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 編輯過