C#數組,List,ArrayList,Dictionary異同和一些問題

數組

聲明:

長度聲明:

int[] A = new int[數量長度];

長度和內容:

int[] array = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

循環賦值:

int[] p = new int[10];
for (int i = 0; i <= 9; i++)
{
Console.Write("請輸入第{0}個值:",i+1);
p[i] = int.Parse(Console.ReadLine());
}

某個:

A[?] = 123;

方法:

排序:

var query = from n in weight

            orderby n descending //descending 爲倒序

            select n;

去重複:

String[] str = {"aa","kser","bdf","ope","aa"};
String[] str2 = str.Distinct().ToArray();

輸出:

for (int i = 0; i < nums.Length; i++) 

 

List和Dictionary

1.  Dictionary的value(可能重複),轉變爲List<>該怎麼操作(主要檢查重複。。)

for (int index = 0; index < 5; index++)
    {
        if (testList.ContainsKey(key[index]))
        {
            testList[k].Add(val[index]);
        }
        else
        {
            testList.Add(key[index], new List<long>{val[index]});
        }
    } 


2.如何刪除list或dic的元素(因爲其index會自動移位)

倒敘遍歷或者continue而不++

Dictionary

dic用迭代器的時候 不能對Key增刪改,因爲迭代器是隻讀所以不能改,因爲長度關係 所以Key不能刪掉

但是通過Linq可以ToList就能刪除了

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章